Pionters

Hey everyone ! I was trying to make a program that sorts various strings. I succeeded but when i tried with pointers I failed.
See please let me know how do make a character pointer move from one row to the other while pointing at ... suppose a 20X20 string.
So if I understand correctly, you have 20 strings, each with 20 characters? If you want to replace each string with another, and YOU'RE SURE that they all have the same length, then you have to do it letter by letter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const int length = 20;

char* str1 = new char[length];
char* str2 = new char[length];
char tempChar; //single character to store buffer characters.

str1 = "abcdefghijklmnopqrst";
str2 = "hellohellohellohello";

for(int i = 0; i < length; i++)
{
    tempChar = str1[i];
    str1[i] = str2[i];
    str2[i] = tempChar;
}

//now here you have str1 and str2 swapped 


Hope that helps :)
Last edited on
1
2
3
4
5
char* str1 = char[length];
char* str2 = char[length];
//...
str1 = "abcdefghijklmnopqrst";
str2 = "hellohellohellohello";

?!?
.
Last edited on
I think what EssGeEich was getting at was this is not legal.
1
2
char* str1 = char[length];
char* str2 = char[length];


Think you meant something like...

1
2
3
4
char buff1[length] = "abcdefghijklmnopqrs";
char buff2[length] = "hellohellohellohell";
char* str1 = buff1;
char* str2 = buff2;


But this does a character by character copy, isn't this much faster (unless I am missing something....

1
2
3
4
5
6
7
8
9
//...
const char* str1 = buff1;
const char* str2 = buff2;
const char * temp = 0; 

temp = str1;
str1 = str2;
str2 = temp
//... 
Last edited on
It's a deprecated conversion but it's conceptually legal... all compilers accept it with a warning. Try it!
I copy and pasted the code, tried it in VS2010 using platform toolset v100 and I got this:

error C2062: type 'char' unexpected
Not really... it's not faster. It's just the same! you're using a built in function. The problem is that the guy's defined the length and hasn't used null terminated strings. So we can't guarantee that strcpy would work.
God I forgot the word "new"... lol :P. I'm correcting it!!!
Last edited on
God I forgot the word "new"... lol :P. I'm correcting it!
hehe.

No built in functions are used in the above example, simply assigning pointer values, take a closer look.
This line:

 
char* mystr = "Hello buddy!";


will always issue a warning with me. At least under g++.
Yes it doesn't' like string literals and char, however, const char is ok.
That is why i got confused, because you missed the "new" operator.
And, you should use const char * (as clanmjc said).
And anyways, it will result in a Memory Leak.
Why?
Because you first assign to the var mystr1 (length) characters.
Then you again assign and overwrite the var mystr1 with a const char * variable.
What you probably meant is strcpy.

I think anyways we are missing the op's question.
Personally i didn't understand it, so if our op could "clean up" his question, I will be easily able to help him.

(What I mean is, You cannot "switch" constant pointers. What the op asked is to switch pointers, so, are you using 'new' for declaring your pointers?)
Last edited on
Topic archived. No new replies allowed.