3

I study C language and I have some difficulty to understand pointers and arrays.

In tutorial that I read I have this row:

 char* arrP1[] = { "father","mother",NULL }; 

And my question is what is arrP1?

Is it array of pointers to the static strings:

enter image description here

Or it is pointer to the string array:

enter image description here

I am confused...what is arrP1?

13
  • Try using the clockwise/spiral rule and see what you get. Commented May 17, 2018 at 22:20
  • 2
    Also, the initializer list should be a pretty good hint, as it contains pointers. Commented May 17, 2018 at 22:22
  • @Someprogrammerdude: I think you might be missing something here. Normally you would see char arrp1[] or char* arrp1, both of which essentially mean the same thing. char* arrp1[] means something else. Commented May 17, 2018 at 22:24
  • 2
    @RobertHarvey I know what char *arrp1[] means. And char *arrp1 and char arrp1[] = { ... } are semantically very different. Commented May 17, 2018 at 22:26
  • 1
    @RobertHarvey char *x = "foo"; and char x[] = "foo"; are semantically very different Commented May 17, 2018 at 22:31

3 Answers 3

3

arrP is an array of char * which in this case is an array of size 3, and you've assigned the pointers to c-style strings with initial values of {"father", "mother", NULL}, which themselves are character arrays that are null terminated. So, your first answer is correct.

Sign up to request clarification or add additional context in comments.

3 Comments

So, your second answer is correct -- Because... ? Your description suggests to me the first answer.
You describe the first answer (which is correct). The second answer would be string(*)[], which doesn't work because C doesn't have a string type.
ah ya, my bad @RobertHarvey
2

To find answer for such declerations, you can use cdecl. It'll highly likely answer you.

declare arrP1 as array of pointer to char

However, there is a something that is called as spiral rule. It can also help you to read decleration. For example,

char *str[10]

         +-------+
         | +-+   |
         | ^ |   |
    char *str[10];
     ^   ^   |   |
     |   +---+   |
     +-----------+

-   str is an array of 10 elements
-   str is an array of 10, of pointers
-   str is an array of 10, of pointers, of type char

4 Comments

Note that the spiral rule is a simplification and many people find it more confusing that helpful. I prefer to go with "Start with the identifier, read from inside out, starting to the right (and of course obeying parentheses if there are any)". In char *str[10], that gives str, then [10], then *, then char, so str is an array of ten pointers to char.
"Start with the identifier, read from inside out, starting to the right (and of course obeying parentheses if there are any)". In char *str[10], that gives str, then [10], then *, then char, so str is an array of ten pointers to char is what actually spiral rule is. @DanielH
In that case, as the first answer in the question I linked says, you redefine spiral. The shape you're following for char **x[10][20][30] looks nothing like a spiral.
ops, yes, that's right. It should be paid attention. Thanks for it. @DanielH
1

Not sure if this will help or make things more confusing but arrP1 can be both an array of char* and a char**, like so:

void foo1(char** arr) { cout << arr << endl; }
void foo2(char* arr[]) { cout << arr << endl; }

int main() {
  char *arr[] = {"a", "b"};
  cout << arr << endl;

  foo1(arr);
  foo2(arr);
  return 0;
}

The interesting thing (that I just found out myself) is that foo2 doesn't create a copy of the array on its stack, it's passed arr directly! All 3 cout print the same address.

Related SO Q&A

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.