Help with strcat

This is a discussion on Help with strcat within the C Programming forums, part of the General Programming Boards category; I am trying to use the strcat function, but I am trying to concatenate numbers and nodes from a struct ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    12

    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
    Posts
    362
    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 :-)
    Code:
    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
    Code:
    char *toString(const Node *p)
    {
      sprintf(strFinal, "%d", p->value);
    
      return strFinal;
    }
    *Cela*

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, 01: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, 05:10 PM
  4. strcat makes program crash
    By imoy in forum C++ Programming
    Replies: 4
    Last Post: 05-09-2002, 03:41 PM
  5. Removing 'strcat' ed string.
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-14-2001, 12:09 AM

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