Thread: invalid conversion from `char*' to `char'

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    151

    invalid conversion from `char*' to `char'

    This is a snippet of code
    Code:
    FROM THE MAIN FUNCTION:
    
    Push(s1,check,line); /*s1 is a structure with elements 	Element *contents;
                                                            int top;
    	                                                int lineST;
                          check is a string array
                          line is an integer */
    
    PUSH FUNCTION ACCESSED FROM ANOTHER C FILE:
    
    void Push(stackST *stackP, char* element, int line)
    {
    
      /* Put information in array; update top. */
      char newelement[20];
      int i=0;
      int length = strlen(element);
      for (i=0;i<=length;i++)
    	newelement[i++]=putchar(*element++);
      newelement[i] = '/0';
     stackP->contents[++stackP->top] = newelement;
      stackP->lineST=line;
    }
    I get the following warning and error for the line marked in bold
    warning: multi-character character constant
    error:invalid conversion from `char*' to `char'

    I have twisted the code a lot. But cant figure the solution to this problem.
    Last edited by Ron; 06-14-2008 at 11:30 AM.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Apparently Element::contents is a char pointer or char array, and when indexing that, you get a char. Newelement is a char array.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    151
    yes you are right, contents is a pointer.
    hmmm....
    so what would the syntax be if i were to point it to newelement?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    (1) The null character is \0, not /0.
    (2) If you want to change the pointer, just use the pointer. I have no idea what pointer you're trying to change -- stackP[top].contents, perhaps?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > for (i=0;i<=length;i++)
    > newelement[i++]=putchar(*element++);
    Why does this increment i twice each time?

    > stackP->contents[++stackP->top] = newelement;
    Assuming you fix the left to be a char*, you're still pointing at a local variable - this is a big no-no

    You need to use malloc to allocate newelement if you want it to persist longer than the life of this function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    151

    Re:

    I have already done so when initializing the stack
    Code:
    void StackInit(stackST *stackP)
    {
      stackElement *newContents;
    
      /* Allocate a new array to hold the contents. */
    
      newContents = (stackElement *)malloc(sizeof(stackElement) * 10);
    
      stackP->contents = newContents;
      stackP->lineST = 0;
      stackP->top = -1;  /* I.e., empty */
    }
    Thanks for pointing out about the increment.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What's a stackElement
    Perhaps you're meant to strcpy it?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    151
    stackelement is a type-defined character defined.

    Also, for some reason the error now doesnt show up anymore in the push function.
    But the error comes up in the line of code that calls the push function.
    Code:
    Push(s1,check,line); /*s1 is a structure with elements 	Element *contents;
                                                            int top;
    	                                                int lineST;
                          check is a string array
                          line is an integer */
    error: invalid conversion from `char*' to `stackElement'
    error: initializing argument 2 of `void Push(stackST*, stackElement, int)'

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Apparently stackElement is not a char *. So again: what is it?

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    151
    I get so impatient with UNIX.......

    I dont know what the 2nd argument is supposed to be a string.
    I dont know why it refers to stackelement

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    This has nothing to do with UNIX and everything to do with your program. Look at your function: void Push(stackST*, stackElement, int). That second argument says stackElement. How did you define stackElement, and what do you want it to be?

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    151
    I figured what went wrong. I gave the wrong parameter in the explicit function call in the header file. Thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. invalid char to char conversion?
    By kryptkat in forum Windows Programming
    Replies: 2
    Last Post: 09-27-2007, 05:16 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. invalid conversion from `const char*' to `char'
    By GameGenie in forum C++ Programming
    Replies: 6
    Last Post: 08-01-2005, 05:30 AM
  5. invalid conversion from `char*' to `char'
    By Tommy7 in forum C++ Programming
    Replies: 3
    Last Post: 06-09-2005, 10:45 PM