Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C variable declaration

Tags:

c

What is the difference between

char *a[10];

and

char *(a[10]); 

I've always used the first for an array of char pointers, but now I've found code used the second.
As I was not sure if it was the same thing, I printed sizeof() both and both return 80 (64bit OS) so I'm inclined to believe both are the same (an array of char pointers).

But as I cannot find any explanation online or anything using *([]) syntax, I was looking for some confirmation.

Thanks

like image 399
Filipe Pina Avatar asked Oct 12 '11 15:10

Filipe Pina


People also ask

How do you declare a variable in C?

type variableName = value; Where type is one of C types (such as int ), and variableName is the name of the variable (such as x or myName). The equal sign is used to assign a value to the variable.

What is declaration in C with example?

Declaration of a function provides the compiler with the name of the function, the number and type of arguments it takes, and its return type. For example, consider the following code, int add(int, int); Here, a function named add is declared with 2 arguments of type int and return type int.

Which is a valid C variable declaration?

A variable name can only have letters (both uppercase and lowercase letters), digits and underscore. The first letter of a variable should be either a letter or an underscore. There is no rule on how long a variable name (identifier) can be.

How do you declare a variable?

Declaring (Creating) Variablestype variableName = value; Where type is one of Java's types (such as int or String ), and variableName is the name of the variable (such as x or name). The equal sign is used to assign values to the variable.


2 Answers

The two are equivalent and stand for a 10-element array of pointers to char.

Contrast with char (*a)[10], which is a pointer to a 10-element array of char.

If in doubt, one could use cdecl to unscramble C declarations. On Unix it's usually available as a command-line tool. There's also an online version.

like image 128
NPE Avatar answered Oct 18 '22 18:10

NPE


char *a[10]; and char *(a[10]); are the same. To make it clear lets say it this way, char *a[10]; is pointer array of 10 and later is array of 10 where all are pointers! You see that both are same with different way of saying that.

like image 23
COD3BOY Avatar answered Oct 18 '22 20:10

COD3BOY