Thread: Strings and Structures

  1. #1
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222

    Strings and Structures

    I have a structure with a string defined inside it, is there a way to change the string later in my program....

    CODE....
    //define structure
    struct myStruct {
    char name[40];
    int age;
    };

    //function to fill array of structures with values
    void fillArray(struct myStruct *);

    int main(void){
    //create array of structs
    struct myStruct myStructArray[10];

    //put values in array of structs
    fillArray(&myStructArray);

    //print name from struct
    printf("%s", myStructArray[2].name);
    }

    //fills array with values
    void fillArray(struct myStruct * workwithStruct){
    workwithStruct[2].name = "JOHN";
    return;
    }


    if i print the name inside the fillArray function it will print fine but when I return to the main function it prints garbage

    I'm new to structs, Can someone please help me out?
    "Assumptions are the mother of all **** ups!"

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    workwithStruct[2].name = "JOHN";

    This is wrong. You have to use something like strncpy to fill the string.

    Furthermore, you're accessing your pointed to structure entirely incorrectly. That's not how you access members of a structure via a pointer.

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

  3. #3
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    I wonder how the previous code compiled : the argument passed to the function fillarray was of the wrong type. The operator = does not copy strings in C; you have to use the function strcpy.
    It is a good idea to include the appropriate headers.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    struct myStruct {
    char name[40];
    int age;
    };
    
    void fillArray(struct myStruct *);
    
    int main(void){
    struct myStruct myStructArray[10];
    fillArray(myStructArray);
    printf("%s", myStructArray[2].name); 
    return 0;
    }
    
    void fillArray(struct myStruct * workwithStruct){
    strcpy( (workwithStruct+2)->name,"JOHN");
    return;
    }
    The one who says it cannot be done should never interrupt the one who is doing it.

  4. #4
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222
    Thankyou, that was all the help i needed

    I am new there was no reason to tear me down for doing it wrong, not everyone starts an expert!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Strings in Structures.
    By SlyMaelstrom in forum C++ Programming
    Replies: 15
    Last Post: 10-12-2005, 04:29 PM
  2. Replies: 2
    Last Post: 04-13-2003, 08:40 PM
  3. Structures Arrays and Char Strings
    By Crocksmyworld in forum C Programming
    Replies: 5
    Last Post: 01-19-2002, 11:56 PM
  4. Comparing strings w/in structures
    By vehl in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2001, 10:59 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM