1,105,371 Community Members

is char** val the same as char* val[ ]

Member Avatar
otengkwaku
Junior Poster
107 posts since Mar 2012
Reputation Points: 20 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 10 [?]
 
1
 

is char** val the same as char* val[ ] when is the formal ( char** val) used

Member Avatar
deceptikon
Eternally Awesome
4,690 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 688 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
2
 

is char** val the same as char* val[ ]

Only as formal parameters in a function definition. Otherwise, they're not. I'm assuming we're talking about parameters for the answer to your next question.

when is the formal (char** val) used

I'll use the array notation when I'm absolutely sure that the function is intended to work with an array or simulated array. I prefer to use pointer notation when the object is known to not be an array or whether it's an array is unknown.

Member Avatar
otengkwaku
Junior Poster
107 posts since Mar 2012
Reputation Points: 20 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 10 [?]
 
0
 

so if i get you, they are all array notation but char** val is implicite and
char* val[] is explicite
because i just noticed that using int main (int argv, char** argc) worked the same way as int main (int argv, char* argc[])

Member Avatar
deceptikon
Eternally Awesome
4,690 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 688 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
0
 

they are all array notation but char** val is implicite and char* val[] is explicite

It's the other way around. They are all pointers, and the [] option is syntactic sugar to highlight the intention that the parameter is an array. But also note that this equivalence only applies to the first dimension of a the array notation. All subsequent dimensions require a size and represent array types. Consider this:

int main()
{
    int a[2][4];

    foo(a);
}

foo() must be declared in one of two ways:

void foo(int a[][4]);  /* First dimension decays into a pointer */
void foo(int (*a)[4]); /* Explicitly stating the first dimension as a pointer */

It's a common misconception that the double pointer type corresponds to a 2D array, which is false:

void foo(int **a); /* Fails to compile */
Member Avatar
otengkwaku
Junior Poster
107 posts since Mar 2012
Reputation Points: 20 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 10 [?]
 
0
 

ok ok ok thanks a lot
question solved

Question Answered as of 1 Year Ago by deceptikon
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: