1

The issue is this. I want to be able to create my argv using array notation:

 char myargv[10][30];

however main functions expect **char despite being able to treat them the same logically Ex:

int main(int argc, char **argv)
{
    printf("%s\n",argv[0]);
    return 0;
}

However trying the following inside my code wont work:

char myargv[10][30];
char **pargv = myargv;

Can somebody clarify? I have also tried some other permutations like, pargv = myargv[0][0] etc, with no success.

EDIT: If this proves to be impossible the way I've outlined can someone help me initialize char **myargv using a malloc call so that it has the 10 strings of 40 characters. Im having trouble getting the syntax right.

4 Answers4

1

this would not work char **pargv = &myargv[0][0];

The reason behind it is that when you try to access/print pargv[1], it does not know how many bytes to skip from the initial position.

instead you should write like this.

char (*pargv)[30] = &myargv[0][0]; \\we have created a pointer to a charater array of size 30

See now if you try to access pargv[1], compiler knows that it has to jump 30 bytes to access the location.

even if this is possible, people go with dynamic allocation as it seens clear and understandable.

following code should work.

char ** pargv;
pargv = (char **)malloc(10*sizeof(char *));

for(int i=0;i<10;i++)
{
  pargv[i]=(char *)malloc(30*sizeof(char));
}

above code works like this.

  • first it creates 10 charater pointers(which will point to a char array);
  • then for each char pointers it allocates 30 bytes of memory(character array of 30 bytes).

conclusively, you have created 10 character arrays. now you can access pargv exactly like you access char pargv[10][30];

Dhaval
  • 1,046
  • 6
  • 10
  • Could you add an explanation of why this should help? It may be right (or not), but it doesn't explain _why_ it is right (or not), and what the original problem was. It's unlikely that a user with a similar problem would find this answer by searching. – Joshua Taylor Oct 02 '13 at 19:09
  • my apologies. changing the answer to the correct one with explanation. – Dhaval Oct 02 '13 at 20:55
  • I only noticed it because it came up in the "low quality answer" review queue, because it's a very short answer, and it's mostly code. – Joshua Taylor Oct 02 '13 at 21:12
  • changed the answer. Thanks. – Dhaval Oct 02 '13 at 21:13
0
char myargv[10][30];
char **pargv1 = myargv;  // Error, in an expression array is converted to pointer
char (*pargv2)[30] = myargv; // OK, pointer to an array 30 of char

pargv1 is a pointer to a pointer to char.

myargv is a an array of array of char. When evaluated it is converted to a pointer to an array 30 of char.

ouah
  • 142,963
  • 15
  • 272
  • 331
0

It doesn't work because myargv is not a pointer, it's an array. Also, lvalue-to-rvalue conversion doesn't apply with assignment.

David G
  • 94,763
  • 41
  • 167
  • 253
  • Ok if there is no way to do this the way I have done it can you help me initialize char **myargv using a malloc call so that it has the 10 strings of 40 characters. Im having trouble getting the syntax right. – user2839711 Oct 02 '13 at 18:36
  • @user2839711 I believe you can do: `char **pargv = reinterpret_cast(myargv);` – David G Oct 02 '13 at 18:37
  • This project has to be in C reinterpret_cast is a c++ operator. If this was C++ I could also solve this using the new operator, but I have to either malloc the space on the heap or assign it from an array on the stack. – user2839711 Oct 02 '13 at 19:09
  • /you shouldn't mislead us with the c++ tag. Anyway, just do the cast as `char **pargv = (char**)myargv;` – David G Oct 02 '13 at 19:11
0

If you are looking for a simple way to declare argv and are not hung up on the idea that argv is a doubly dimensioned array (It's not, it's a singly dimensioned array of pointers), then

char* argv[] = {
"one", "two", "three", "four", 0};

would work (ignoring const issues)

David G
  • 94,763
  • 41
  • 167
  • 253
Dale Wilson
  • 9,166
  • 3
  • 34
  • 52