2

I have a function:

int exploreDIR (char stringDIR[], char arguments[6][100])
{    
     /*stuff...*/
     execv(filePath, arguments); 
}

However, I get the warning: passing argument 2 of ‘execv’ from incompatible pointer type

If execv expects char* const argv[] for its second argument, why do I receive this warning?

Since arrays are essentially the same thing as pointers to the start of the array, what is the critical difference here between char arguments[][] and char* const argv[]?

Gangadhar
  • 10,248
  • 3
  • 31
  • 50
Dan Brenner
  • 880
  • 10
  • 23
  • 3
    No wrong in `execv(filePath, arguments); `, `arguments` should be NULL terminated but `arguments[6][100]` can't be NULL terminated as `arguments[i]` is a valid address != NULL – Grijesh Chauhan Oct 15 '13 at 19:12
  • I'm confused, what is the solution here? I thought that by making arguments of size 6 (of which I only ever planned to fill 5 of them) that the last value would be a NULL pointer and thus cause the termination. Is this wrong? – Dan Brenner Oct 15 '13 at 19:16
  • 1
    Read: [Difference between `char* str[]` and `char str[][]` and how both stores in memory?](http://stackoverflow.com/questions/17564608/what-does-the-array-name-mean-in-case-of-array-of-char-pointers/17661444#17661444) – Grijesh Chauhan Oct 15 '13 at 19:16
  • even if you fill only first five argument (from `argument[0]....argument[4]` ) even then `argument[5]` is not equals to NULL it would be a valid address with strlen = 0, read the answer I linked. – Grijesh Chauhan Oct 15 '13 at 19:23

2 Answers2

1

char arguments[6][100] is a 600-byte chunk of memory arranged into 6 100-byte segments, whereas char* argv[] is an array of pointers to segments of memory which could be anywhere. One way to see the difference: arguments[i+1] - arguments[i] will be 100, while argv[i+1] - argv[i] could be ANYTHING.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
1

You are are passing a pointer (*) looking like this:

*
`-> aaaa...aaaabbbb...bbbbcccc...cccc

It points to memory containing several char[100] arrays.

The function expects an argument looking like this:

*
`->***
   ||`-> cccc...cccc
   |`-> bbbb...bbbb
   `-> aaaa...aaaa

It wants a pointer pointing to memory containing several char*.

The two types are different and cannot be automatically converted.

sth
  • 222,467
  • 53
  • 283
  • 367