Could anyone tell me what it means to increment a pointer or array?
e.g. char* arrayOfStrings[10]; //array of strings, obviously
//...some code
arrayOfStrings++ //-- what does this mean??
//can I increment an array?
Could anyone tell me what it means to increment a pointer or array?
e.g. char* arrayOfStrings[10]; //array of strings, obviously
//...some code
arrayOfStrings++ //-- what does this mean??
//can I increment an array?
Assume
char *str = "string";
and
char *ptr;
and
ptr = str;
then
ptr++;
results in ptr pointing to t. An array is a block of memory existing of smaller blocks. In this case, we have a block of memory existing of smaller blocks which are bytes (char). So incrementing the pointer which points to the start of the block, results in the pointer pointing to the next block.
*str is just a char pointer. That means:Code:char *str = "string";
str is the address of the first character of the string.
*str is the first the value of it (in this case 's').
You can increment the pointer and then it points at the next value
(in this case 't').
if str points to the first character of a string you can write
*(str+6)
for
str[6]
itīs exactly the same.
Here is an example for pointer incrementation:
klausiCode:unigned int lenofstring(char *string) /* returns the length of a string */ { char *temp; temp=string; while(*string!='\0') *string++; return string-temp; }
When I close my eyes nobody can see me...