Thread: Help with strcat

  1. #1
    Registered User
    Join Date
    Feb 2003

    Help with strcat

    I am trying to use the strcat function, but I am trying to concatenate numbers and nodes from a struct into the string. I have a struct that looks like this:

    typedef struct node{
    struct node * left;
    struct node * right;
    int value;
    } Node;

    My function toString appears like this:

    char *toString(const Node p)
    strcat(strFinal, p.value);

    I realize that I can't put an int value into the string, but casting is not working. All the values are decided in my main program, and I know the values are being passed because I did some printf tests. When I try to cast it, I get the warning:

    lab1.h:37: warning: passing arg 2 of `strcat' makes pointer from integer without a cast

    which in turn, creates a segmentation fault. I really appreciate any help. Thanks!

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Your compiler is right, converting an int to a string is bad stuff unless you do it properly. Now, assuming that strFinal is where you want the number put and it's already waiting, this should work for you :-)
    char *toString(const Node p)
      sprintf(strFinal, "%d", p.value);
      return strFinal;
    And you really should consider passing p as a pointer, it's more efficient that way and the changes are minimal
    char *toString(const Node *p)
      sprintf(strFinal, "%d", p->value);
      return strFinal;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An interesting question about strcat
    By meili100 in forum C++ Programming
    Replies: 3
    Last Post: 07-07-2009, 12:59 PM
  2. strcat the inside of a structure
    By ebullient in forum C Programming
    Replies: 3
    Last Post: 12-09-2005, 05:58 PM
  3. strcat and a char
    By jjacobweston in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2005, 04:10 PM
  4. strcat makes program crash
    By imoy in forum C++ Programming
    Replies: 4
    Last Post: 05-09-2002, 02:41 PM
  5. Removing 'strcat' ed string.
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-14-2001, 12:09 AM