Archived

This topic is now archived and is closed to further replies.

need help with pointers again

This topic is 5151 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, How would I go about copying a char pointer into another one correctly? Lets say we have:
char *stringA, *stringB;
stringA = Name;
stringB = secName;
 
.. I want to append stringB to stringA how would I do that without having a nice crash?

Share this post


Link to post
Share on other sites
use strcpy(). but make sure that the pointers have enough space in them to put the other characters in. The reson youd get a crash with something like this is becuase of buffer overruns. Make sure the pointers point to a chuck iof sufficietn size and you wont get a crash (as long as you dont tread out of the box)

or you can use std::string and your problems are solved quite easily ie:

std::string a, b;
a = "this";
b = "what";
a += b;

now a is equal to "thiswhat"

[edit] *beats self with bat* not strcpy, strcat [/edit]


:::: [ Triple Buffer ] ::::


[edited by - ifoobar on November 8, 2003 3:16:44 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Tiffany_Smith
strcat() should do it.



Yeah that''s what I''ve been trying to do but it crashes I''m guessing that''s because I''m overrunning the array or something I dunno...

quote:

use strcpy(). but make sure that the pointers have enough space in them to put the other characters in. The reson youd get a crash with something like this is becuase of buffer overruns. Make sure the pointers point to a chuck iof sufficietn size and you wont get a crash (as long as you dont tread out of the box)



How would I do that? An example might help thanks

Share this post


Link to post
Share on other sites
If you want to append, strcpy() won''t work because It will erase the content of the current buffer.

The crash you are experiencing is probably due to memory, like IFooBar suggested. So continue to use strcat() and make sure you have space and it should be fine.

Share this post


Link to post
Share on other sites
quote:
Original post by NullPointer33

quote:

use strcpy(). but make sure that the pointers have enough space in them to put the other characters in. The reson youd get a crash with something like this is becuase of buffer overruns. Make sure the pointers point to a chuck iof sufficietn size and you wont get a crash (as long as you dont tread out of the box)



How would I do that? An example might help thanks




char a[32] = "this"
char b[32] = "what"

strcat( a, b );


:::: [ Triple Buffer ] ::::

Share this post


Link to post
Share on other sites

char *a = "string a", *b = "string b", *c = 0;

// allocate enough room for the length of both strings, plus 1 for null character.

if(!(c = malloc(strlen(a)+strlen(b)+1)))
{
// out of memory. . .

}

sprintf(c, "%s%s", a, b);

// c is now a and b stuck together.


// free c when you''re done with it.

free(c);

Share this post


Link to post
Share on other sites

to make a copy

size_t length = strlen(Name);
char *stringB = malloc(length+1);
if ( NULL == stringB ) {
//bail out or something
}

strcpy(stringB, Name);

or

strncpy(stringB, Name, length+1);

don''t forget to free stringB

to concatentate

size_t lengthA = strlen(stringA);
size_t lengthB = strlen(stringB);
char *stringC = malloc(lengthA+lengthB+2);
if ( NULL == stringC ) {
//bail out or something
}

strncpy(stringC, stringA, lengthA+1);
strncat(stringC, stringB, lengthB);

don''t forget to free stringC


Share this post


Link to post
Share on other sites
quote:

char a[32] = "this"
char b[32] = "what"

strcat( a, b );



No...that's not what I mean I understand how to append it to another string normally,using strcpy/strcat. You see the problem is I need it to be a pointer is there a way to do it like this without having a crash? I'm trying to make a class for practice to modify the string etc, I have a function that passes a pointer to the class and I need to take that pointer and append it to the string pointer the class is using. Is there a way to do that?

EDIT: Ah ok that should work...thanks

[edited by - NullPointer33 on November 8, 2003 3:40:54 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hi,
I just want to say that using string class would be a better idea. But, here is how you could go about messing with strings (which are just arrays of chars). I tested this with MSVC 6 and it works like a charm. If you already have a char* that points to a valid string, then you do not need to alloc or dealoc mem. But you must ensure that the result buffer is large enough to hold both strings. You could use a function like strlen(stringA) to retrieve the size. One thing you must remember, strings are just an array of bytes. you could just use a memcopy to copy from string to string.

#include "stdio.h"

int main(int argc, char* argv[])
{
char *stringA, *stringB, *result;

//First off, you need to allocate memory for new strings
stringA = new char[5];
stringB = new char[10];

//Make a result buffer big enough to hold the two strings
result = new char[ 20 ];

//Lets put something into stringB
sprintf( stringB, "012345678" );

//Heres how you could assign one char at a time
stringA[0] = ''j''; //"hello"
stringA[1] = ''e'';
stringA[2] = ''l'';
stringA[3] = ''l'';
stringA[4] = ''\0'';


//Now, one way to append them to make A+B=AB
sprintf( result, "%s%s", stringA, stringB );

//Should say "hello0123456789"
printf( result );
//since we allocated mem for these, we need to clean up reverse of how we allocated
delete result;
delete stringB;
delete stringA;

return 0;
}

Share this post


Link to post
Share on other sites
Thanks I finally got it to work I didn't know about sprintf() really nice function

Now that we are on the subject where could I find a tutorial or some sort of info on the c++ string class I'd like to learn how to use it but most of the information I get online deals with char arrays only it's really hard to find anything related to that...


[edited by - NullPointer33 on November 8, 2003 6:25:56 AM]

Share this post


Link to post
Share on other sites
Doing things to strings is even easier in C++:


#include <iostream>
#include <string>

int main()
{
// create our strings

std::string a = "this is ", b = "a string";

// append b to a (same as ''a = a + b'')

a += b;

// print a so you can see what happened

std::cout << a << std::endl;
}

Share this post


Link to post
Share on other sites
quote:
Original post by smart_idiot
// create our strings
std::string a = "this is ", b = "a string";

As we are tutoring here, I must point out that the slightly more correct way is:

std::string a("this is "), b("a string");

EDIT: Anyone else always get std::String?

[edited by - CWizard on November 8, 2003 6:47:07 AM]

Share this post


Link to post
Share on other sites