Thread: more on struct.

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    19

    more on struct.

    how to print struct from another function?
    let's say I have code this way
    Code:
    struct student{
    int id // simple example
    };
    main(){
    modify_data(); // calls the function.
    }
    lode_struct_function()
    {
    // here I load the struct value from the file.
    struct student line[i] // i comes from another function.
    line[i].id = student_id;
    }
    modify_data()
    {
    struct student line[i]  // ? ?????
    printf("%d",line[i].id); // here is the key how can i print line[i].id on this function?
    }
    Last edited by programinproces; 10-31-2013 at 12:35 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Make use of a pointer, e.g.,
    Code:
    #include <stdio.h>
    
    struct student
    {
        int id;
    };
    
    void modify_data(struct student *the_student)
    {
        the_student->id = 1;
    }
    
    int main(void)
    {
        struct student a_student = {0};
        modify_data(&a_student);
        /* print the struct, etc */
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    19
    Thanks. But I have another function witch loads the struct value from the line of text file.
    Code:
    int load_struct()                                        //this function loads structs element from the file.{
        int x=0;
        int i=count(x);
        struct student line[i];
        char file_line[100];
        char* line_parts;
        
        FILE * fp;                                                          // file opens here.
        if ((fp = fopen ("file.txt", "r")) != NULL)
        {
            while (x<i) {
                fgets(file_line,100,fp);     // gets line from the file.
                
                
                line_parts = strtok(file_line,",");
                line[x].st_id  = atoi(line_parts);
                
                
                line_parts = strtok(NULL, ",");
                strcpy(line[x].st_name,line_parts);
                
                line_parts = strtok(NULL, ",");
                line[x].st_age = atoi(line_parts);
                
                line_parts = strtok(NULL, ",");
                strcpy(line[x].st_dep,line_parts);
                
                line_parts = strtok(NULL, ",");
                line[x].st_grade = atoi(line_parts);
                x++;
                
            }
            
            
        }
        fclose(fp);
    
    
        return  0;
    }
    Now I have to use these value in modify_data function. I only call load_struct and modify_data function in the main?
    Sorry I am new and not doing much in the pointer. Thanks again.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Notice that you store the data of the file in an array of structs, which is locally declared, thus will go out of scope when the function terminates.

    I think an example like this is what you need to get you boosted.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    19
    SO, if I declare this line as a global variable does that work?
    struct student line[i];
    this way.
    Code:
    struct student{
       int id;
    }line[i]; // to do this way i am getting complain about [i].
    Last edited by programinproces; 10-31-2013 at 02:54 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Are you wanting the function to fill in the whole array, or just one single item/struct/object in that array?

    In either case the array should exist in the calling function, not the called function, and certainly not globally; and you should pass the relevant thing into your function. (If you just want to fill in a single object, you would maybe need to pass-by-address so that the contents can be modified.)

  7. #7
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by tabstop View Post
    Are you wanting the function to fill in the whole array, or just one single item/struct/object in that array?

    In either case the array should exist in the calling function, not the called function, and certainly not globally; and you should pass the relevant thing into your function. (If you just want to fill in a single object, you would maybe need to pass-by-address so that the contents can be modified.)
    Yikes. I know it's probably a typo or simplification but there is no such thing as "pass-by-address" in C. I'd modify the statement to read "If you just want to fill in a single object, you would maybe need to a pointer to the object so that the contents can be modified."

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by SirPrattlepod View Post
    Yikes. I know it's probably a typo or simplification but there is no such thing as "pass-by-address" in C. I'd modify the statement to read "If you just want to fill in a single object, you would maybe need to a pointer to the object so that the contents can be modified."
    "Pass the address"? "Pass-by-pointer"? You can pick whichever you like. (I think "pass-by-pointer" is probably most common, though? And the CS people probably like "pass by reference", although then you can get into an argument with the C++ people.)

  9. #9
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by tabstop View Post
    "Pass the address"? "Pass-by-pointer"? You can pick whichever you like. (I think "pass-by-pointer" is probably most common, though? And the CS people probably like "pass by reference", although then you can get into an argument with the C++ people.)
    But in C everything is pass-by-value.

  10. #10
    Registered User
    Join Date
    Oct 2013
    Posts
    19
    Thanks all for your valuable suggestion, Now I'm getting little higher in the pointer. Can you please, suggest me for my illusion about the pointer?
    lets say, I have function called fn1(), fn2() and fn3(). I need to do this.
    Code:
    fn1()
    {
    fn2() // simply i call fn2() here
    fn3(*fn2()) // is that it pass by reference ?????
    }
    fn2()
    {
    //something and something
    }
    fn3(what i put here?)
    {
    //something or manything 
    }
    is that possible to do ?
    Last edited by programinproces; 11-01-2013 at 01:32 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  2. 2 problems: Struct array and pointer + struct
    By v1n1c1u5 in forum C Programming
    Replies: 0
    Last Post: 12-13-2009, 05:38 PM
  3. struct holding data inside a linked list struct
    By icestorm in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 12:49 PM
  4. Replies: 1
    Last Post: 05-05-2004, 06:58 AM
  5. returning a pointer of a struct of a struct array...
    By myrddinb in forum C Programming
    Replies: 1
    Last Post: 04-13-2004, 06:49 PM