Sign in to follow this  
Mizipzor

String question

Recommended Posts

Mizipzor    247
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! :)

Share this post


Link to post
Share on other sites
Simian Man    1022
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.

Share this post


Link to post
Share on other sites
Oluseyi    2103
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.

Share this post


Link to post
Share on other sites
Mizipzor    247
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.

Share this post


Link to post
Share on other sites
Drew_Benton    1861
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].

Share this post


Link to post
Share on other sites
Zahlman    1682
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this