-2

What is difference between ASturct arr[] and ASturct* arr[]?

ASturct array[]
ASturct * array[]

Thanks for answer!

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Emil Mein
  • 33
  • 1
  • 5
  • 2
    Do you know what is `char ` and `char *`? – haccks Nov 16 '16 at 17:51
  • yeah I know but what is the difference? char is type of variable and char* is a pointer of char type – Emil Mein Nov 16 '16 at 17:52
  • 1
    @EmilMein No, both `char` and `char*` are types. Different types. Same with `ASturct` (whatevfer that is) and `ASturct*`. Two types, but two different types. You can a variable of type `ASturct` and another variable of type `ASturct*`. – Some programmer dude Nov 16 '16 at 17:53
  • Now think both just as data types and answer your own question. – haccks Nov 16 '16 at 17:53
  • Oh, and unless you're declaring `array` as an argument to a function, you can't have an empty size for arrays. – Some programmer dude Nov 16 '16 at 17:55
  • Finally, you probably need to [find a good beginners book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and start reading from the beginning. – Some programmer dude Nov 16 '16 at 17:56
  • @Some programmer dude But if you create array(first or 2nd) i will get pointer to the first element and elements are represented by pointers (like from definition array is a set of pointers). – Emil Mein Nov 16 '16 at 17:58
  • No you are misunderstanding how arrays work. They can *decay* to a pointer to its first element. But an array is not by itself a pointer. And each element in an array is a *value* and not a pointer (unless the value *is* a pointer, like in the second example you show). – Some programmer dude Nov 16 '16 at 18:04

2 Answers2

1
ASturct  array[]

Above one is array of type ASturct .

ASturct * array[]

And this one is array of pointers (which are of type ASturct).

GorvGoyl
  • 42,508
  • 29
  • 229
  • 225
0

You seem to have a basic misunderstanding about how arrays and pointers work.

Lets take a very simple example:

int a = 1;
int b = 2;
int c = 3;

int array1[3] = { a, b, c };

The above code creates an array of three int values. In memory it will look something like

+---+---+---+
| 1 | 2 | 3 |
+---+---+---+

The variables a, b and c are not anywhere in the array, the values of those variables were copied into the array when the array was initialized.

Now lets take a very different example:

int a = 1;
int b = 2;
int c = 3;

int *array2[3] = { &a, &b, &c };

Now we have an array of pointers, and initialize each element to point to the variables a, b and c. It will look something like this in memory

+----+----+----+
| &a | &b | &c |
+----+----+----+
 |    |    |
 |    |    v
 |    |    +---+
 |    |    | 3 |
 |    |    +---+
 |    v    
 |    +---+
 |    | 2 |
 |    +---+
 |
 v
 +---+
 | 1 |
 +---+

Both array1 and array2 in the example above are array with three elements, but the similarities end there. The type and value of each element in the arrays are very different from each other. For array1 each element is a single int, it holds a number (like e.g. 1 for array1[0]). For array2 each element is a pointer to an int value, and its value is the address of that int value.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621