strcat the inside of a structure

This is a discussion on strcat the inside of a structure within the C Programming forums, part of the General Programming Boards category; i want to concatenate a string to a structure passed in the parameter and return the new structure. and it ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    8

    strcat the inside of a structure

    i want to concatenate a string to a structure passed in the parameter and return the new structure. and it works if i pass it by reference and use pointers,

    for example,

    Code:
    typedef struct
    {
        char someString[80];
    }someStruct;
    
    
    
    someStructMore(someStruct);
    
    int main
    {
        someStruct blah;
        /*etc assignments to blah*/
        someStruct more(someStruct blah);
        return 0;
    }
    
    
    someStruct more(someStruct blah);
    {
        someStruct value;
        char aLine[80] = "Heya person.";
    
      ....
    
    }
    And what I'd like to do is

    strcat(blah.someString, aLine);
    strcat(more.someString, blah.someString);

    But I get compiling issues. I understand that strcat concatenates a string to a pointer, meaning I can't just use blah.someString or more.someString, so I am now wondering how I'd be able to get around that issue but using something other than strcat(). Any ideas?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    strcat( foo.bar, "something" ); /*directly*/
    strcat( foo->bar, "something" ); /*via a pointer*/

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    8
    I understand how to use the pointer option, but I want to change the actual value without having to use it.

    I get compiling issues if I do,

    Code:
    strcat(foo.bar, "string");

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    It's easier to start from a real example rather than a paraphrased one.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct type
    {
        char text[80];
    };
    
    struct type foo(struct type copy)
    {
       struct type newobj;
       strcpy(newobj.text, copy.text);
       strcat(newobj.text, "strcat ");
       return newobj;
    }
    
    int main(void)
    {
        struct type a = {"start "};
        struct type b = foo(a);
        struct type c = foo(b);
        puts(a.text);
        puts(b.text);
        puts(c.text);
        return 0;
    }
    
    /* my output
    start 
    start strcat 
    start strcat strcat 
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing pointer to a structure in a function
    By steve1_rm in forum C Programming
    Replies: 5
    Last Post: 02-03-2008, 01:48 AM
  2. Replies: 13
    Last Post: 12-14-2007, 02:34 PM
  3. Problem with arrays inside a structure
    By babu in forum C Programming
    Replies: 4
    Last Post: 07-12-2007, 09:35 AM
  4. Structure inside Structure
    By tdep in forum C Programming
    Replies: 11
    Last Post: 07-05-2007, 08:23 AM
  5. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 05:17 AM

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