Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between char a[]="string"; char *p="string"; [duplicate]

Tags:

c

Possible Duplicates:
What is the difference between char s[] and char *s in C?

What is the difference between char a[]="string"; and char *p="string";?

like image 761
Blue Sky Avatar asked May 30 '10 13:05

Blue Sky


People also ask

What is the difference between char A () string and char * p string?

char a[]="string"; // a is an array of characters. char *p="string"; // p is a string literal having static allocation. Any attempt to modify contents of p leads to Undefined Behavior since string literals are stored in read-only section of memory.

What is the difference between char * p and char * p?

char* p and char *p are exactly equivalent. In many ways, you ought to write char *p since, really, p is a pointer to char . But as the years have ticked by, most folk regard char* as the type for p , so char* p is possibly more common.

What is difference between char * and char?

The main difference between them is that the first is an array and the other one is a pointer. The array owns its contents, which happen to be a copy of "Test" , while the pointer simply refers to the contents of the string (which in this case is immutable).

What's the difference between char and string?

char is a primitive data type whereas String is a class in java. char represents a single character whereas String can have zero or more characters. So String is an array of chars. We define char in java program using single quote (') whereas we can define String in Java using double quotes (").


2 Answers

The first one is array the other is pointer.

The array declaration "char a[6];" requests that space for six characters be set aside, to be known by the name "a." That is, there is a location named "a" at which six characters can sit. The pointer declaration "char *p;" on the other hand, requests a place which holds a pointer. The pointer is to be known by the name "p," and can point to any char (or contiguous array of chars) anywhere.

The statements

char a[] = "hello";
char *p = "world";

would result in data structures which could be represented like this:

   +---+---+---+---+---+---+
a: | h | e | l | l | o |\0 |
   +---+---+---+---+---+---+
   +-----+     +---+---+---+---+---+---+
p: |  *======> | w | o | r | l | d |\0 |
   +-----+     +---+---+---+---+---+---+

It is important to realize that a reference like x[3] generates different code depending on whether x is an array or a pointer. Given the declarations above, when the compiler sees the expression a[3], it emits code to start at the location "a," move three past it, and fetch the character there. When it sees the expression p[3], it emits code to start at the location "p," fetch the pointer value there, add three to the pointer, and finally fetch the character pointed to. In the example above, both a[3] and p[3] happen to be the character 'l', but the compiler gets there differently.

You can use search there are tons of explanations on the subject in th internet.

like image 101
Incognito Avatar answered Sep 22 '22 21:09

Incognito


char a[]="string"; //a is an array of characters.

char *p="string";// p is a string literal having static allocation. Any attempt to modify contents of p leads to Undefined Behavior since string literals are stored in read-only section of memory.

like image 42
Prasoon Saurav Avatar answered Sep 21 '22 21:09

Prasoon Saurav