Click here to Skip to main content
15,883,749 members
Please Sign up or sign in to vote.
1.65/5 (6 votes)
See more:
1)What is difference between char *STR="hello"; and char arr[]="hello";

2)What is difference between c++ structure and c structure ??? :)
Posted
Updated 12-Dec-16 20:17pm
v3
Comments
pasztorpisti 24-Feb-14 20:35pm    
Both STR and arr allocate a char array in a global memory area or on the stack containing the characters on the stack. The differences:
1. arr: arr is an identifier whose value is a constant, there is no variable allocated for the value of arr itself. When you use arr in your code then the compiler "hardcodes" this constant. (Under the hood it isn't necessarily a constant when the machine code is generated for offset independent stuff and when the location of the array is a stack relative memory area but this is the problem of the code generator, from your perspective (from the perspective of the language user) arr is a constant value without allocated variable/storage.)
- STR: In case of this declaration besides the character array the compiler also allocates a char* variable for you and initializes it for you to the address of the character array (to the constant value that is the same value you access with the arr symbol in the previous example). A very big difference compared to arr is that STR here itself is a pointer VARIABLE that you can modify later in your code, you can not do that with the arr CONSTANT.

Another very big difference: the type of the STR and arr symbols. The type of STR is char* while the type of arr is char[]. The difference is often invisible because the C/C++ compilers contain a very ugly *hack* (in my opinion): array identifiers (arr) are automatically casted to pointers in many cases. For example if you say char* myvariable = arr; then the compiler automatically casts the type of arr into a pointer that points to arr.

sizeof also has surprises: sizeof(STR) is the same as sizeof(void*) as STR is a pointer VARIABLE and sizeof(arr) will give you the size of the character array as arr is a character array. However if you declare a function like:
void myfunc(char array_param[])
{
}
then sizeof(array_param) is sizeof(void*) because array parameters are pointers in reality, array parameters are never passed by value even if you specify the exact size of the array parameter!!! You can enforce a fixed size for an incoming array parameter with the static keyword (I never used it, just seen it in a weird example...)

The ugly similarities between arrays and pointers should not exist in C/C++, modern languages eliminated these to prevent programming mistakes but C/C++ will probably keep them forever because of backward compatibility.

1) STR is a pointer to character(s), initialized with the constant character sequence {'h','e','l','l','o', \0'}. you may, in following statements of your program change its value, for instance:
char c='a'; STR = &c;

On the other hand, arr is an array of characters, initialized with the same constant sequence of characters.
STR and arr are similar and often interchangeable, anyway there are differences, for instance,
sizeof(STR) != sizeof(arr).

2)In C structs are a (powerful) mean for grouping data, i.e. they contain just member variables. On the other hand, C++ structs are fully qualified classes (having data members and methods - constructor and destructor included -).
The difference between a class and a struct in C++ language is just the default visibility level (by default, members are public in structs while they are private in classes).
:)
 
Share this answer
 
Comments
Richard MacCutchan 13-May-11 6:44am    
Nice clear answer. +5
CPallini 13-May-11 6:50am    
Thank you, Richard.
I was young and skillful when I wrote it. :-D
Albert Holguin 13-May-11 11:40am    
great answer! my 5
Stefan_Lang 24-Feb-14 6:40am    
Have another 5. *thunbsup*
CPallini 24-Feb-14 6:51am    
Tank you, is unexpected (but welcomed) on a rather old answer. :-)
Do some searching first before you ask a question. There are answers available. :)
Regarding the first one, take a look at http://c-faq.com/decl/strlitinit.html[^]
second one: http://www.geekinterview.com/question_details/37280#8[^]
 
Share this answer
 
v2
Comments
Alain Rist 5-Mar-11 3:34am    
Edited second link to point to the correct answer.
1)What is difference between char *STR="hello"; and char arr[]="hello";
-> The first one is a pointer to character array which you can increment to point to the next character where as in the array you can not increment the base pointer.
-> Sizeof str will be 4 as it is a pointer in 32 bit compiler,but for arr it will be 5.


2)What is difference between c++ structure and c structure ??? Smile | :)
->In C while declaring the variable of a structure type,struct keyword is required to be prepended unlike in C++.
->C++ structures can have member functions, I guess C does not support that.
 
Share this answer
 
v2
Comments
Da Bai Cai 13-May-11 7:21am    
Sizeof str will be 4 as it is a pointer in 32 bit compiler,but for arr it will be 5.
the answer is wrong,sizeof(arr)=6,including '\0'
hakz.code 14-May-11 1:13am    
Thanks for correcting mate,I forgot the NULL character..!
Usman Hunjra 24-Feb-14 11:57am    
clever .. :)
Q1:you know STR is a pointer, arr[] is an array,but they have the same usage :)
Q2:C++ structure can be regarded as the expansion of C structure
 
Share this answer
 
Comments
Piccadilly Yum Yum 5-Mar-11 4:36am    
i agree
Stefan_Lang 24-Feb-14 6:37am    
A1 is false. You can reassign a pointer to a different address, but you cannot reassign an array variable. Moreover you can determine the size of the array of an array variable, but that is not possible for a pointer.

A2 is rather vague and doesn't answer the question. For reference, solution 3 offers a much more concise and correct answer.
1] In pointer case , as Hello has 5 words thus 5 byte of memory is consumed to store it .
and each byte will have its different memory address , each incremented by one from the previous one .Here *str is a pointer to character which will point to 'H' of hello . In other words *str holds address of 'H' . It is important to note that str is a pointer and it can point to only one address at a time , and thus it points to'H' not to Hello .

whereas arr[] is a array . it is like name provided to the characters as a[0] ='H' , a[1]=e and so on ..

What the main difference comes is in pointer characters where accessed by their address whereas in they are accessed by their variable name . if you want detailed information on function working , click it and click it if you want detailed information on arrays .


2] C Structure :-
1. Only variables of different data types can be declared, functions are not allowed

2. Direct access to data members is possible

3. ‘struct’ data type is not treated as built in type – use of ‘struct’ necessary to declare

objects

4. Member variables cannot be initialized inside a structure.
struct tag
{
member 1 declaration;
member 2 declaration;
...
...
member m declaration;
};
C++ Structure :-
1. In C++ structure declaration functions can also be declared

2. The members declared in a C++ structure is public by default

3. While declaring an object the keyword ‘struct’ is omitted in C++
 
Share this answer
 
Comments
Richard MacCutchan 13-Dec-16 4:38am    
Please do not post in dead questions; especially those that already have full answers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900