String question

Started by
4 comments, last by Zahlman 19 years ago
What I want to is this: char a[] = "abc"; char b[] = "def"; char c[] = strcat(a, b); I.e. make a string, make another then patch them together and store in a third one. I get errors from this. I think the problem is the way arrays and pointers work together. Ive tried a few ways but I dont get it right, cant find the info I need in my book either. I turn to the community now, help me out guys! :)
Advertisement
The problem is that arrays are constant pointers.

char* a = "abc"; //is valid

char a[] = "abc" //is not valid

You cannot change the address of a char array, but you can change where a pointer points to. To do what you want, you can also say:

char a[4];

strcpy( a, "abc" );

Or, (preferably in my opinion) you can use std::string

std::string a = "abc"
std::string b = "def"

sdt::string c = a + b;

This is perfectly valid.
I hope this made sense - I know it's confusing.
Quote:Original post by Simian Man
char a[] = "abc" //is not valid
Wrong.

The problem is that no memory has been allocated to c.

@mizipzor:
The errors your compiler gives you are informative. They have an error code which corresponds to an entry in the documentation, which usually explains the error as well as provides common causes and solutions. In essence, everything you needed to solve your problem was sitting in front of you.
Oluseyi: Yeah I know, Ive read the error messages. They say "cant add two pointers", "illegal conversion between array and pointer", "const pointer cant...". Thing is, I dont really know when a pointer is an array and when an array is a pointer. The real trouble is, they can be both depending on how I use them... I think at least.

Simian Man: Im reading your post over and over... Im trying to write some code and see if it takes me somewhere... then come back here to look for more posts. :P Its true as they said, pointers are the first real challenge for beginners.
Quote:Original post by mizipzor
Oluseyi: Yeah I know, Ive read the error messages. They say "cant add two pointers", "illegal conversion between array and pointer", "const pointer cant...". Thing is, I dont really know when a pointer is an array and when an array is a pointer. The real trouble is, they can be both depending on how I use them... I think at least.


How about taking a look at an example of using the strcat function. [smile].
Quote:Original post by mizipzor
Oluseyi: Yeah I know, Ive read the error messages. They say "cant add two pointers", "illegal conversion between array and pointer", "const pointer cant...". Thing is, I dont really know when a pointer is an array and when an array is a pointer. The real trouble is, they can be both depending on how I use them... I think at least.


They never are.

An array name can be used as a constant pointer to the beginning of the array storage. And conversely, a pointer can be pointed at the beginning of an array - or the middle of one, or off into the middle of nowhere.

Anyway. Those aren't strings, dammit.

#include <string>using std::string;string a = "abc";string b = "def";string c = a + b; // easy to read, and it works! No fuss, no muss.

This topic is closed to new replies.

Advertisement