Re: How to increment array of pointers to strings



On 04/08/10 18:37, barncat wrote:
I am trying to increment the pointer of this array, similar to
incrementing *argv[]:
*++argv
printf("%s\n", *argv); /* now prints the arg at argv[1]; */
--
code:
char *args[8];
args[0] = "x";
args[1] = "y";
args[2] = "z";

/* if i use:
args++; or
*args++;
i get error:
xxx.c:18: error: wrong type argument to increment
*/
--
how can i increment the pointer and print the contents of the current
location of it?

You're declaring an array of pointers to char. Try walking through the
following code:

#include <stdio.h>

int main(int argc, char *argv[])
{
char * args[8];
int i,j;

args[0] = "tiger";
args[1] = "dog";
args[2] = "horse";
args[3] = "pig";
args[4] = "cougar";
args[5] = "lion";
args[6] = "elephant";
args[7] = "leopard";

i = 0;

while (i < 8)
{
j = -1;
while (args[i][++j] != 0x00);
printf("%s has length %d\n",args[i++],j);
}

return 0;
}

Rgds

Denis McMahon
.



Relevant Pages

  • Re: Memory Structure Pointer Problems
    ... typedef struct sta { ... char* name; ... int num_cmpnds; ... A pointer to a struct cmp is almost ...
    (comp.lang.c)
  • Re: Insufficient guarantees for null pointers?
    ... will the compiler know what the bounds are after converting that char * ... to an int *, if it could point to either of two arrays which happen to ... compares equal to the original pointer. ...
    (comp.std.c)
  • Re: problem with memcpy and pointers/arrays confusion - again
    ... int line, unsigned long *total_mem) ... That's a long pointer address... ... If sizeof > sizeof which is ... if you allocate for char with sizeof < sizeof, ...
    (comp.lang.c)
  • Re: Request critique of first program
    ... Returns a pointer to an asplit_result struct (unless unable to ... Success is indicated by SUCCESS, ... const char *out_file_b, ... const long int num_lines); ...
    (comp.lang.c)
  • Re: static in [ ]
    ... void someFunc (int size, char a) ... and passing a pointer to fewer than size characters is fine. ...
    (comp.lang.c)