Hi,
Can any body tell me the exact diff between char * str and char str[] in C ?
What exactly happens in memory when we declare these ?
Regards,
Naik
Printable View
Hi,
Can any body tell me the exact diff between char * str and char str[] in C ?
What exactly happens in memory when we declare these ?
Regards,
Naik
It's the same. Both create a variable "str" which is a pointer to char.
There is a topic on this already: Variable Pointer VS Array (Wai Wai) which answers your question.
There's a difference. Have a look at this thread. :thumb:
Thanks for highlighting. SuperKoko 's answer is correct. :thumb:Quote:
Originally Posted by exterminator
"Variable Pointer VS Array" show the difference between char[] and char* for local variable declarations, but you must know that it is very different for argument declarations.
That is, for arguments there is no difference between char[] and char*:
Is identical to:Code:int f(char str[])
{
std::cout<<str;
str[0]='\0'; // modify the original string
}
These two functions are identical, so you cannot use overloading:Code:int f(char *str)
{
std::cout<<str;
str[0]='\0'; // modify the original string
}
Moreover if you try to have a linker error "undefined function", like that:Code:int f(char []);
int f(char *); // compiler error : "int f(char *)" already declared.
The linker (ilink32.exe, but i suppose that it may depend on the linker - i think that ilink32.exe is revelant on this point) outputs:Code:// test.cpp
int f(char []);
int main()
{
char str[8];
f(str);
return 0;
}
It does not outputs 'f(char [])', but 'f(char *)'Code:Error: Unresolved external 'f(char *)'
You can also put any positive integer between the '[' and the ']' characters, and there is always no difference.
What you must absolutely know, is that you cannot pass an array by value.
If you want to do so, you should define a struct containing the fixed size array:
On this point, i think that C++ is inconsistent (probably for compatibility with C which was also inconsistant on this point), so i never use this syntax, but only use the "char *" syntax.Code:struct CharArray8 // Obsolete - nowadays you should use std::vector - it is much better!
{
char Array[8];
};
If you want to do some different processing on arrays and pointers, you must use a reference to an array such as this template does:
Code:template <size_t n>
size_t StringSize(char (&CharArray)[n]) {return n-1;} // returns the size of an array of characters minus the null terminator.
I just discovered this thread you should read.
It contains more informations on argument arrays.
well the most difference is the obvios one:
char str[] is an chararray ( memory allocation)
while char * str is an pointer to an memorydestination( doesn#t allocate
memory) ;-)
Ummm...Quote:
Originally Posted by rene1
where does "str2" get stored then, if not in memory?Code:char str1[] = "str1";
char* str2 = "str2";
The two are slightly different. The nuance is this:Quote:
Originally Posted by Bond
This would declare an array large enough to hold the string literal used as the rvalue. This array is initialized with the string's value. Therefore, there is a 4-byte array declared here containing 's', 't', 'r', and '\0'. This array has read/write access and can be modified without any issue. str1 contains the address of the first character.Code:char str1[] = "str1";
In this case, we are declaring a pointer to a character. This pointer is initialized to point to the beginning of a string literal. This literal may be stored in a read-only section of the program. I wasn't actually aware that this would compile without a warning, as you're initializing a non-const pointer to point to a const string literal. str2 points to the first character of the string literal, which may or may not actually be writable.Code:char *str2 = "str2";
fact is any array of anything is just a pointer. The array syntax just allows you the functionality to associate memory with that pointer. Since they're both pointers the syntax for addressing them is interchangable.Code:char *ptr = "zipy zippy zoo";
char ptr2[] = "blah blah blah";
char ptr3[15] = "zip zip bip";
foo ( char *myptr)
{
printf( "%s\n", myptr );
}
int main()
{
char *ptr4 = new[15];
strcpy( ptr4, "shazam");
foo(ptr);
foo(ptr2);
foo(ptr3)
foo(ptr4);
delete []ptr4;
}
base[] == *base derefferenceCode:
typedef struct MYSTRUCT
{
int i;
char c;
char z[20];
} MYSTRUCT, *PMYSTRUCT;
int main()
{
MYSTRUCT myArray[10];
PMYSTRUCT pMyArray = new MYSTRUCT[10];
myArray->i = 1; // set's the first value of the array
pMyArray->i = 1; // set's the first value of the pointer memory
(myArray+2)->i = 1; // set's the third item of the array
(pMyArray+2)->i = 1; // set's the third item of the pointed to memory
myArray[2].i = 3; // set's the third item of the array
pMyArray[2].i = 3; // set's the third item of the pointed to memory
delete []pMyArray;
}
base[#] == *(base+offset) add an offset to the base and dereffrence
arrays are just pointers with memory already associated with them. Pointers are just arrays which may or may not have the memory associated with them.
The only difference I know of is one which SuperKoko touched upon..
You can't change the base address of an array. Pointers you can..
char szbuffer[10] = "";
char *pszBuffer = new char[10];
szBuffer += 1; // ilegal
pszBuffer += 1; // legal
An array contains an address, and a pointer contains an address.
The rest is just syntax.