char to char*?

This is a discussion on char to char*? within the C Programming forums, part of the General Programming Boards category; Hello, sorry if I'm asking a lot of questions, but is there any way to convert a char to char*? ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    32

    char to char*?

    Hello, sorry if I'm asking a lot of questions, but is there any way to convert a char to char*?

    I'm reading a string one by one, and if i find a certain character i want to push it to a stack, however my stack contains a char*.. how do i do this? :\

    Code:
    while(*x!='\0')
        {
            if(*x=='(')
    		{
    			char* temp = *x;
                push(&(*top),temp);
    		}
    ..
    obviously this doesn't work..neither does this

    Code:
     if(*x=='(')
    {
    	char* temp;
    	strcpy(temp,*x);
            push(&(*top),temp);
    }
    I tried this, but i'm not sure if its correct?
    Code:
      if(*x=='(')
    {
    	char temp[2] ="(";
    	char *p = temp;
    
           push(&(*top),p);
    }

  2. #2
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    You cannot really "convert" a char * to a char, since a char * is a pointer to a value, and a char is a value itself. But you can assign the value pointed to by a char * to a char, or vice versa convert a char * to point to a value of another char.

    Code:
    char *a;
    char b = 'c';
    
    a = &b; /* A now points to b */
    *a = b; /* Put the value of b in whatever a is pointing to */
    Also &(*top) does not make any sense, since you are first dereferencing a pointer and then taking the address with the &-operator, so they cancel out. &(*top) is the same as just top.
    Last edited by iceaway; 10-05-2011 at 06:20 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-08-2009, 01:47 AM
  2. Difference between char *str1 & const char *str2
    By Tigers! in forum C Programming
    Replies: 4
    Last Post: 08-06-2009, 04:04 AM
  3. Read File To Char Array with Null char init
    By MicroFiend in forum Windows Programming
    Replies: 1
    Last Post: 10-28-2003, 05:18 PM
  4. Assigning Const Char*s, Char*s, and Char[]s to wach other
    By Inquirer in forum Linux Programming
    Replies: 1
    Last Post: 04-29-2003, 10:52 PM
  5. unsigned char vs signed char and range of values
    By Silvercord in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2003, 12:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21