Converting char* to char

Hi,
I am trying to convert a char pointer to a normal char(somehow copy the strings from the pointer and pass it to the char)

Here is a piece of example on what i want to do
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
    char *namen="Test";
    char test[255]

    test=namen;
    cout<<test<<endl;

    return 0;
}


If i run that code i get the following error : "invalid conversion from char** to char"

I know you cant just copy a char pointer to a char but is there a way or a technique to do it?
Thanks in advanced.
Last edited on
closed account (jwC5fSEw)
A char* is equivalent to a char array. A char is just a character literal. You can't put in a string in a char.
Hmmm i'm sure there must be a way that thats possible, is there anyway to copy the characters for the pointer to a char?
char can only hold 1 letter nothing more.

include <string>

if you don't want to deal char*/char[] arrays
I think you are misunderstanding a few basics here....

#1: I think you're confusing a pointer with a string. Pointers are not strings.

#2: a 'char', as its name suggests, holds a single character. Strings consist of multiple characters. Therefore you can't "convert" a string to a char because a char isn't large enough to hold an entire string.

What you can do is take an individual character out of the string and hold it in the char, but I doubt this is what you're going for.


Basically:

You're dealing with strings here, so use strings, not chars:

1
2
3
string test = "Test";  // <- #include <string>

cout << test;


EDIT: doh, too slow
Last edited on
Ok i replaced the char thing(did not notice that at all, thanks for bringing that up)
But thats not the real problem, what i am trying to do is to copy the char's from a char pointer and pass it to a char.
In other words i want to somehow copy the characters that the pointer points to a normal array of characters
closed account (jwC5fSEw)
A char* IS a char array. Saying char* c = "Hello world!" is equivalent to saying char c[] = "Hello world!".
Last edited on
Ok let me bring the real problem up, i am trying to make a way to detect when my Usb drive is inserted and i am using this 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
#include <iostream>
#include <windows.h>
using namespace std;

int main()
{

    char VolumeName[MAX_PATH+1];
    char DriveLetter[] = "A:\\";

    for (char count = 'A'; count <= 'Z'; ++count)
    {
        DriveLetter[0] = count;
        if (GetVolumeInformation(DriveLetter,VolumeName,MAX_PATH+1,NULL,NULL,NULL,NULL,0) != FALSE)
        {

            if (VolumeName=="MARGIN")
            {
                cout<<"Drive is in"<<endl;
            }

            cout << VolumeName << endl;
        }
    }


    return 0;
}


For some reason it prints out in the name of the drive but it doesn't print out "Drive is in"
I cant figure out why and i think its because its not comparing the char* with "MARGIN" correctly.
Yes, you can't compare C-strings like that. You should just switch to std::strings, which actually have == overloaded.

If for some unknown reason you HAVE to use c-strings (why?), then you can use strcmp().
You can't compare char arrays with the equality operator. Or rather, you can, but it's highly unlikely to work.
strcmp() is used, instead:
1
2
3
4
char *s0,*s1;
strcmp(s0,s1)<0 //s0<s1. In other words, s0 should appear first in a dictionary.
strcmp(s0,s1)>0 //s0>s1
strcmp(s0,s1)==0 //s0==s1 
Remember that strcmp(), as all C string functions, understands that the string ends at the first '\0' from the pointed-to character.

Oh, and a char is not a character literal. A literal is a type of token; a constant entered in the code. For example,
 
int a=1234+'A';
Here there's four types of tokens. Two identifiers (int, a), two operators (=, +), two literals (1234, 'A'), and a syntactical token (;).
closed account (jwC5fSEw)
To convert a string to a char for the purposes of function arguments, use c_str().
strcmp(VolumeName=="MARGIN")
Does that look anything like what I posted?
strcmp(s0,s1)==0 //s0==s1
closed account (jwC5fSEw)
Wait did a post just get deleted?
Sorry, i deleted the post...i somehow did the mistake of strcmp(VolumeName=="MARGIN")
(i gues it was a busy day)...
Thanks for all of you help.
And i gues i have to pick my c++ book again :/
Last edited on
Topic archived. No new replies allowed.