Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pointers and string parsing in c

I was wondering if somebody could explain me how pointers and string parsing works. I know that I can do something like the following in a loop but I still don't follow very well how it works.

  for (a = str;  * a;  a++) ...

For instance, I'm trying to get the last integer from the string. if I have a string as const char *str = "some string here 100 2000";

Using the method above, how could I parse it and get the last integer of the string (2000), knowing that the last integer (2000) may vary.

Thanks

like image 905
Robert74 Avatar asked Jun 27 '10 14:06

Robert74


People also ask

What is string and pointer in C?

String is a data type that stores the sequence of characters in an array. Every string terminates with a null character (\0), indicating its termination. A pointer to a string in C can be used to point to the base address of the string array, and its value can be dereferenced to get the value of the string.

What is string parsing?

You could say that parsing a string of characters is analyzing this string to find tokens, or items and then create a structure from the result. In your example, calling Integer. parseInt on a string is parsing this string to find an integer.

Can pointers be used on strings?

In C, a string can be referred to either using a character pointer or as a character array.

What are the pointers in C language?

The pointer in C language is a variable which stores the address of another variable. This variable can be of type int, char, array, function, or any other pointer. The size of the pointer depends on the architecture. However, in 32-bit architecture the size of a pointer is 2 byte.


1 Answers

for (a = str; * a; a++) ...

This works by starting a pointer a at the beginning of the string, until dereferencing a is implicitly converted to false, incrementing a at each step.

Basically, you'll walk the array until you get to the NUL terminator that's at the end of your string (\0) because the NUL terminator implicitly converts to false - other characters do not.

Using the method above, how could I parse it and get the last integer of the string (2000), knowing that the last integer (2000) may vary.

You're going to want to look for the last space before the \0, then you're going to want to call a function to convert the remaining characters to an integer. See strtol.

Consider this approach:

  • find the end of the string (using that loop)
  • search backwards for a space.
  • use that to call strtol.

-

for (a = str; *a; a++);  // Find the end.
while (*a != ' ') a--;   // Move back to the space.
a++;  // Move one past the space.
int result = strtol(a, NULL, 10);

Or alternatively, just keep track of the start of the last token:

const char* start = str;
for (a = str; *a; a++) {     // Until you hit the end of the string.
  if (*a == ' ') start = a;  // New token, reassign start.
}
int result = strtol(start, NULL, 10);

This version has the benefit of not requiring a space in the string.

like image 116
Stephen Avatar answered Sep 19 '22 09:09

Stephen