Difference between char a[]=“string”; char *p=“string”; [closed]?

Up vote 6 down vote favorite share g+ share fb share tw.

C link|improve this question edited May 30 '10 at 17:43James McNellis120k12299474 asked May 30 '10 at 13:57Blue Sky312.

– liori May 30 '10 at 13:59 2 This is a duplicate – Johannes Schaub - litb May 30 '10 at 14:02 @liori: char a=“s”;! = char *p=“s”;? :) – Pessimist May 30 '10 at 14:03 c-faq.com/decl/strlitinit.html – nc3b May 30 '10 at 14:09 Exact duplicate of What is the difference between char s and char *s in C?

– James McNellis May 30 '10 at 14:21.

The first one is array the other is pointer. The array declaration "char a6;" 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 x3 generates different code depending on whether x is an array or a pointer.

Given the declarations above, when the compiler sees the expression a3, it emits code to start at the location "a," move three past it, and fetch the character there. When it sees the expression p3, 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 a3 and p3 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.

– Ram Bhat May 30 '10 at 14:54 3 +1 for ASCII artwork. – Thomas Matthews May 30 '10 at 17:15 1 @Ram Bhat: in the first case, it depends on where that definition is placed; if it's in a function, probably on the stack, if it is in a struct, it depends from where the whole struct is allocated; if it's outside any function, in the global vars segment. The same holds for the pointer p; the "world" string to which p points, instead, usually is in a particular section of the executable which is mapped in memory at loading, which is used as string table.

– Matteo Italia May 30 '10 at 18:59.

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.

First declaration declares an array, while second - a pointer. If you're interested in difference in some particular aspect, please clarify your question.

No difference. Unless you want to actually write to the array, in which case the whole world will explode if you try to use the second form. See here.

One difference is that sizeof(a)-1 will be replaced with the length of the string at compile time. With p you need to use strlen(p) to get the length at runtime. Also some compilers don't like char *p="string", they want const char *p="string" in which case the memory for "string" is read-only but the memory for a is not.

Even if the compiler does not require the const declaration it's bad practice to modify the string pointed to by p (ie *p='a'). The pointer p can be changed to point to something else. With the array a, a new value has to be copied into the array (if it fits).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions