I have a problem with char vs char*

I dont know much about the difference beteween the two but here is what I am trying to achieve

1-I want to take a letter array and transfer the data on over to another letter array.
2-Below is just a simple version of what I am trying to achieve, overall I want to do the same process but with structs that contain letter array components.
here is what I did.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;



using namespace std;
char arrayTaker(char* n[]);
 
int main() {
    char* arrayTest[3];
    char* mark[3];
mark[3]= "love";                /// this is my array

arrayTest=arrayTaker(mark);  /// this function is uppose to take my array and copy it on to arrayTest
for (int j=0; j>3; j++)

{cout<< mark[j]<<endl;
}
  system("pause");

  return 0;



}
char arrayTaker(char* n[])  {
     
     char* Test[3];
     
     
     for (int j=0; j>3; j++) {
     Test[j]=n[j];
     }
     return Test[3];
     }      



This is suppose to do a very simple process but It is meant to be part of a larger program which takes in structs that contain names and stores arrays into them.

The error I keep getting has to do with the char* to char conversion (which I dont understand)

please help me.
char is a single character, e.g. 'A'.
char * is an array of characters, a.k.a. C string, e.g. "Hello!".
Line 16 is buggy.
You can't assign any type of array in this fashion, which is trying to convert the right operand -- "love", from char* to the type of the left operand -- the 4th element of the array mark, which is char. That's what the compiler is yelling about.
But you could have initialized a character array on line 15 like this:
char* mark[5] = "love";
Last edited on
When you declare "char A;" what "A" holds is the value of single letter (which could be
a-z , A-Z, or sign letters look up the ASCII table).

When you declare "char B[size]" or "char B[5]" what you have is an array of chars
where "B" is holding the location (memory address) of "B[0]" the first element(or index) of the array . So, when you increase your index, you move to the next block of memory.
example: B[2] = 's';

When you declare "char * C" what this is, is a pointer to a char. It holds the memory address of the char data. C could be pointing to a single char or an array of chars.

C == B[0]
C is actually same thing as "B[0]". The difference is that B already points to an array of char at beginning of life and C is pointing to some garbage unless it is defined where to point to.

So you could actually say "C = B;"
now both B & C is pointing to the same array of chars and you could use either C or B
to access the specific index in array and change or output a letter from the array.
Last edited on
Okay I tried what you told me wjee0910 but that simply doesnt work.

I actually tried converting everything to char* and that did not work.


If anybody can correct every char I have in my script and replace it with char* (when necessary ) then that would be great.

Okay now I try to use strings instead of chars, the good thing is they work when I test them.

the bad news it is it does not work in loops.

check this modified code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <string>
#include <stdio.h>
#include <string.h>






using namespace std;

 
int main() {
 string clients[25];
 

   string empty[25];

clients[0]="john";
clients[1]="Smith";
clients[2];" jr";

empty[0]=clients[0]; // this part actually works

for (int j=1; j>4; j++){
empty[j]=clients[j];      // this loop does not do anything
}

cout<<clients[0]<<" "<<clients[1]<<endl;
cout<< empty[0]<<" "<<empty[1]<<endl;
  system("pause");

  return 0;

}



essential what happens is the "John" part only gets copied whereas the parts Smith and Jr dont.

So how come the loop does not work?

here is an example of having an array of chars and assigning it to another array of chars:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main(){
    char * s = "hello";
    // what "s" is is a pointer to a char, and by initializing it to array of char
    // "hello" it get the memory address of 'h' at position[0] and from there it allows 
    // us to move to the next location just by changing the index of "s".

    for(int i = 0; i <= sizeof(s); i++) // sizeof() gives us the size of the array
	cout << s[i];			// lucklly char is a byte.
    
    char * j = s; // here i am creating another char pointer which gets copy of "s".

    for(int i = 0; i <= sizeof(j); i++){ // here is cout "j".
	if(i == 0)// just a condition to start from new line
	    cout << endl;
	cout << j[i] ;
    }

    return 0;
}


hello
hello


1
2
3
4
5
6
7
//P.S: by saying char * test[3]; what you are doing is creating an array of char pointers 
//     now this pointers could either point a single char or array of chars so you could 
//     acctually have something like:
    test[0] = "john";
    test[1] = "JR";
    test[2] = "smith";
// in this case you could output this with nested loop. 
1
2
3
4
5
6
7
//P.S: by saying char * test[3]; what you are doing is creating an array of char pointers 
//     now this pointers could either point a single char or array of chars so you could 
//     acctually have something like:
    test[0] = "john";
    test[1] = "JR";
    test[2] = "smith";
// in this case you could output this with nested loop.  



that's almost correct, you just need to use the strcpy or strncpy functions to do the assignment though, you can only do that if you're initializing the char array or pointer. =)

Edit:

I forgot but you might even have to do some dynamic memory allocation first, else each element would be a char pointer which isn't pointing to any memory address?
Last edited on
@Dacster13

you could actually do that without any dynamic memory allocation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main(){		
    
    char * k[3] = {NULL}; // you make your array of pointers point to "NULL" to indicate that it is empty
    k[1] = "This";
    k[2] = "blah";// assigning info after  
    
    char * s[3] = {NULL}; 
    s[0] = k[0]; // here as long as 's' and 'k' are the same size array its okay
                       // and i would do this copying operation in a loop to copy all of "k"'s info.
    
    for(int i = 0; i < (sizeof(k)/sizeof(*k)); i++){
	if(k[i] != NULL){
	    for(int s = 0; s < sizeof(k[i]); s++)
		cout << k[i][s];
    	    cout << endl;
	}
    }
    return 0;
}


This
blah

but yes this only works for char array's and pointers.
I see, would that be an array of char pointers then, or a pointer to char arrays?

 
char *s[3];


That has always confused me somewhat. Well cause I don't think i'ts legal to do something like.

1
2
char *s;
*s = 'a';


Haven't really tried it out though, so I'm not sure.
Last edited on
char * s [3]; // this would be an array of char pointers.

*s = 'a'; // that is illegal because 'a' is not an array of char its just a single char who's value is an int ascii # 95.
*s = 'a'; // that is illegal because 'a' is not an array of char its just a single char who's value is an int ascii # 95.


I know that actually, I think you misunderstood my question though. What I mean is, would it be an array to char pointers? or a pointer to char arrays? Because if it's an array of char pointers then you can probably do something like.

1
2
3
char *s[3];
*(s[0]) = 'a'; // s[0] will give us a pointer so we can dereference it.
                              // if it is an array of char pointers that is. 


which should be equivalent to

*s = 'a'

which I think is probably not legal cause then s[0] would not be pointing to anything? So perhaps it's a pointer to a char array instead of an array of char pointers I mean. I'm not really sure though cause like I said it's a bit confusing to me what it actually is.

Edit:
also if it is an array of char pointers then you should be able to assign memory addresses to it.
Like:

1
2
3
4
char letter = 'a';

char *s[3];
s[0] = &letter; // assign the address of our variable to the first char pointer in the array. 


Theoretically speaking that is. Never really tried it out though, I might try it later on my compiler to see if those indeed work. I just have a bad headache right now though.
Last edited on
Haven't read most of the posts since your last one, berlisen. I just did a bit on skim reading to see if anyone told you why the loop never worked.

But your for loop is wrong:
1
2
3
for (int j=1; j>4; j++){
empty[j]=clients[j];      // this loop does not do anything
}


j = 1, while j is greater than 4, run the statements in the loop, then add 1 to j.

It should be less than 4.
Thanks lynx876

there were also other problems

I have come to conclude that using strings is much easier than using chars.


Topic archived. No new replies allowed.