tags:

views:

203

answers:

5

First off : STRCAT :

Cplusplus - strcat

When clearly the definition says :

char * strcat ( char * destination, const char * source );

Why'd they use char str[80] in the example??? Shouldn't they have used a character pointer?

+3  A: 

That is because arrays decay into pointers in C/C++. If you define char s[80] the value of s will be the address of the first character i.e &s[0]

Naveen
+3  A: 

array can also be used as pointer. what strcat needs is the pointer to a memory in which it copies the destination string. In this case str[80] will give you the memory that can hold 80 chars.

aJ
A: 
char str[80];

Edit: Opps, I rushed my answer. As dribeas says the statement declares an array and str can be implicitly converted into a pointer when it is used. For instance:

++str;

is an invalid operation while:

char* ptr;
++ptr;

isn't.

Patrick
False. It declares an array of 80 chars. It is a common misconception that arrays and pointers are the same, they are not. The reason that the code sample works is that an array will be implicitly converted into a pointer to the first element and that will be a `char*`
David Rodríguez - dribeas
A: 

An array is, strictly speaking, a pointer to the beginning of a block of memory. So str is a char * that points to the beginning of 80 characters.

When you index into an array, say position 53, the following is equivalent: str[53] is the same as *(str + 53) as str is just a char * and adding 53 to a character pointer will return a pointer so to get the value inside you have to use an asterisk to dereference the pointer. In effect array notation just makes code more readable in certain circumstances.

Actually a great little trick with arrays of char is when you want to skip over some leading text when copying a string. E.g. let's say your array str[80] contains the string "1023: error in code!". And you want to display just the string without the number in front. In this case you could say printf( "%s", str + 6 ) and only "error in code!" would be printed.

PP
False, and array is not a pointer to the beginning of a block of memory, but rather the block of memory itself. The compiler will provide you with a pointer to the first element implicitly if required, but they are different. Check: `char array[10];char* p=array;assert(sizeof(array)==10); assert(sizeof(p)!=sizeof(array));`
David Rodríguez - dribeas
In your last example, where you write `str+6` the compiler determines that it cannot add a number to an array, so it implicitly converts it to a pointer to the first element and then performs pointer arithmetic to that pointer. At any rate, arrays always decay into pointers at function calls.
David Rodríguez - dribeas
Ah good point dribeas, you're right, an array is not a pointer but can masquerade as one in certain circumstances. Especially with the sizeof operator. Thanks for the clarification.
PP
+2  A: 
char str[80];

declares an array of 80 characters. However, in C and C++, arrays are implicitly converted to pointers. When you pass an array to a function (such as strcat), it automatically "decays", forming a pointer to the first element of the array.

That's not the same as saying that arrays and pointers are the same thing. They aren't. For example, sizeof() yields different results on the above array, and a char*.

jalf