|
-
August 5th, 2005, 02:14 AM
#1
what is the diff between char str[] and char * str ?
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
-
August 5th, 2005, 02:23 AM
#2
Re: what is the diff between char str[] and char * str ?
It's the same. Both create a variable "str" which is a pointer to char.
-
August 5th, 2005, 02:32 AM
#3
Re: what is the diff between char str[] and char * str ?
There is a topic on this already: Variable Pointer VS Array (Wai Wai) which answers your question.
-
August 5th, 2005, 02:34 AM
#4
Re: what is the diff between char str[] and char * str ?
There's a difference. Have a look at this thread.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
August 5th, 2005, 02:53 AM
#5
Re: what is the diff between char str[] and char * str ?
 Originally Posted by exterminator
There's a difference. Have a look at this thread. 
Thanks for highlighting. SuperKoko 's answer is correct.
-
August 5th, 2005, 05:20 AM
#6
Re: what is the diff between char str[] and char * str ?
"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*:
Code:
int f(char str[])
{
std::cout<<str;
str[0]='\0'; // modify the original string
}
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 []);
int f(char *); // compiler error : "int f(char *)" already declared.
Moreover if you try to have a linker error "undefined function", like that:
Code:
// test.cpp
int f(char []);
int main()
{
char str[8];
f(str);
return 0;
}
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:
Error: Unresolved external 'f(char *)'
It does not outputs 'f(char [])', but '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:
Code:
struct CharArray8 // Obsolete - nowadays you should use std::vector - it is much better!
{
char Array[8];
};
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.
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.
Last edited by SuperKoko; August 5th, 2005 at 05:25 AM.
-
August 5th, 2005, 05:36 AM
#7
Re: what is the diff between char str[] and char * str ?
I just discovered this thread you should read.
It contains more informations on argument arrays.
-
August 5th, 2005, 07:12 AM
#8
Re: what is the diff between char str[] and char * str ?
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) ;-)
-
August 5th, 2005, 11:52 AM
#9
Re: what is the diff between char str[] and char * str ?
 Originally Posted by rene1
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...
Code:
char str1[] = "str1";
char* str2 = "str2";
where does "str2" get stored then, if not in memory?
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
-
August 5th, 2005, 12:17 PM
#10
Re: what is the diff between char str[] and char * str ?
 Originally Posted by Bond
Ummm...
Code:
char str1[] = "str1";
char* str2 = "str2";
where does "str2" get stored then, if not in memory?
The two are slightly different. The nuance is this:
Code:
char str1[] = "str1";
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 *str2 = "str2";
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.
-
August 5th, 2005, 02:53 PM
#11
Re: what is the diff between char str[] and char * str ?
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;
}
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:
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 derefference
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.
Last edited by JMS; August 5th, 2005 at 03:01 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|