1

Would coding:

char *argv[]

would be the same as:

char **argv

Or would:

char *string1;
int  *int_array;

be exactly the same than:

char string1[];
int  int_array[];
alt.126
  • 1,097
  • 1
  • 9
  • 22
  • 5
    It depends..... – wildplasser May 27 '18 at 00:36
  • 1
    Like @wildplasser said, it depends. Mostly, the answer is "No — they are not equivalent". However, when the objects declared are arguments to functions, the answer is sometimes "Yes". You're going to need to learn which is which — and the simple equivalence is wrong, so you should forget that you ever thought of it or heard of it or read about it. It will lead you astray. – Jonathan Leffler May 27 '18 at 00:40
  • 1
    Some or all of these are relevant: https://stackoverflow.com/questions/5727/what-are-the-barriers-to-understanding-pointers-and-what-can-be-done-to-overcome, https://stackoverflow.com/questions/1641957/is-an-array-name-a-pointer, https://stackoverflow.com/questions/10186765/what-is-the-difference-between-char-array-vs-char-pointer-in-c, https://stackoverflow.com/questions/779910/should-i-use-char-argv-or-char-argv-in-c, https://stackoverflow.com/questions/1704407/what-is-the-difference-between-char-s-and-char-s – Jonathan Leffler May 27 '18 at 00:46
  • As function parameters, yes. Anywhere else, no. – Steve Summit May 27 '18 at 05:15

4 Answers4

5

Absolutely not. One is a pointer, the other one is an array.

However, when it comes to parameters, char *param is equivalent to char param[] or char param[16], for example.

From the C11 draft N1570, §6.7.6.3/7:

A declaration of a parameter as "array of type" shall be adjusted to "qualified pointer to type" [...]

cadaniluk
  • 15,027
  • 2
  • 39
  • 67
  • Although most compilers would be able to detect that `param[16]` is an error if and only if it were declared with the size parameter. Also, `char **param` can be reassigned unless declared as `char ** const param`. – Davislor May 27 '18 at 01:32
  • @Davislor: `char* param[]` can be reassigned, too. http://coliru.stacked-crooked.com/a/1e076d1ba715232a – rici May 27 '18 at 04:51
3

There are several contexts where [] could be used. It gets a different treatment depending on the context:

  • Function parameter declaration - here char *argv[] is equivalent to char **argv
  • Array declarations with initialization - here int x[] = {1, 2, 3} is a way to have the compiler count array elements for you
  • Declaration at translation unit scope - here int x[] is treated as a tentative declaration, with storage provided at some other place
  • Last member of a struct - this is a flexible member declaration, which lets you "embed" arrays of variable size into a struct
  • Compound literals - (const int []) {1, 2, 3} lets you construct arrays "on the fly" without declaring a special variable.

As you can see, * and [] get the same treatment only in the first context; in all other contexts the treatment is different.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

No! Well, it depends on the context but explicitly they aren’t the same type.

* is a pointer whereas [] is an array.

A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address. The general form of a pointer variable declaration is * before the varname

Where an array is a list of objects.

I recommend looking up what pointers are as they are a strong part of C and C++.

Here is a good place to learn about them https://www.tutorialspoint.com/cprogramming/c_pointers.htm (also the place I got the quote from above)

1

No.

The concept of arrays is related to that of pointers. In fact, arrays work very much like pointers to their first elements, and, actually, an array can always be implicitly converted to the pointer of the proper type.

Pointers and arrays support the same set of operations, with the same meaning for both. The main difference being that pointers can be assigned new addresses, while arrays cannot.

Read more at cplusplus.

Community
  • 1
  • 1
Lorhan Sohaky
  • 19
  • 1
  • 4