Structs (C)

myStruct.c

#include <stdio.h>

/* Define the struct before main. */
struct student {
  char name[15];
  int id;
};

// Give a synonym. Now struct student is the same with student. 
typedef struct student student;

void input(student*, int);
void output(student*, int);

int main(void)
{
   
    // Declare an array of 3 students.
    student stdArray[3];
    
    input(stdArray, 3);
    
    output(stdArray, 3);
    
    return 0;
}

void input(student* stdArray, int N)
{
    int i;
    
    for(i = 0 ; i < N ; i++)
    {
        printf("Input name of student, please.\n");
        scanf("%15s", stdArray[i].name);
        
        printf("Input ID, please.\n");
        scanf("%d", &stdArray[i].id);
    }
}

void output(student* stdArray, int N)
{
    int i;
    
    for(i = 0 ; i < N ; i++)
    {
        printf("Name of %d-th student: %s\n", i, stdArray[i].name);
        printf("ID of %d-th student is: %d\n", i, stdArray[i].id);
    }
}

This code was developed by me, G. Samaras.

Now we are going to demonstrate, with the help of 3 simple examples for beginners.

arrow0.c

// Example 1
// Referencing a structure member locally in "main()" with the "dot operator"
 
#include <stdio.h>
 
struct Test
{
    int x;
};
 
int main(void)
{
    struct Test sTest;
 
    sTest.x = 2;
 
    printf("x = %d\n",sTest.x);
 
    return 0;
}

arrow1.c

// Example 2
// Referencing a structure member in a function, using the "dot operator"
//    and standard pointer notation
 
#include <stdio.h>
 
struct Test
{
    int x;
};
 
void updateStruct(struct Test *y);
 
int main(void)
{
    struct Test sTest;
 
    sTest.x = 2;
 
    printf("x = %d\n",sTest.x);
 
    updateStruct(&sTest);
 
    printf("x = %d\n",sTest.x);
 
    return 0;
}
 
void updateStruct(struct Test *y)
{
    (*y).x = 3;  // line of interest
}

arrow2.c

// Example 3
// Referencing a structure member in a function, using the "arrow operator"
 
#include <stdio.h>
 
struct Test
{
    int x;
};
 
void updateStruct(struct Test *y);
 
int main(void)
{
    struct Test sTest;
 
    sTest.x = 2;
 
    printf("x = %d\n",sTest.x);
 
    updateStruct(&sTest);
 
    printf("x = %d\n",sTest.x);
 
    return 0;
}
 
void updateStruct(struct Test *y)
{
    y->x = 3;  // line of interest
}

Make sure that you understand what happened in the line of interest at arrow1.c and arrow2.c. These two lines are completely equivalent, but it is a handy way of writing the same thing. It is a shortcut that C provides to the programmer.

These three examples were developed by Matticus of C board. Thanks again Matt!

Notice that with structs, the arrow operator (->) is being used very often. For an example , click here.

In (ANSI) C99, you can use a designated initializer to initialize a structure:

MY_TYPE a = { .flag = true, .value = 123, .stuff = 0.456 };

Source

Have questions about this code? Comments? Did you find a bug? Let me know!😀
Page created by G. (George) Samaras (DIT)

14 thoughts on “Structs (C)

  1. I guess you are referring at the link of List (c) (https://gsamaras.wordpress.com/code/list-c/). One can see the arrow operator at the following lines:
    function: in @line: 70 and 73
    function: n_th @line: 81 and 85
    function: insert_at_start @line: 94 and 95
    function: insert_at_end @line: 103 and 104
    function: delete @line: 111 and 113
    function: print @line: 125
    function: free_list @line: 133
    At one glance, I see them. Hope this helps.
    Thank you for your comment.

  2. Oh sorry, I did not understand that. There is no code with the arrow operator here, but I see your point. There should be. For that reason, I asked for Matticus’s permission to borrow some code of his from C board, where he nicely shows how the arrow operator comes into play. I am going to upload them tonight (might modify them a bit – have the permission for this too). I would also like to thank Matticus, from here too, in advance.

    Thanks also again Whiteflags for pointing that out.

  3. Pingback: checking what the user has inputed

  4. Pingback: Error from "invalid use of undefined type `struct '"

  5. Pingback: Help with Struct and pointers

  6. Pingback: Help??How to solve this error??

  7. Pingback: Uninitialized variables in structure.

  8. Pingback: Size of Linked List

  9. Pingback: Array of structs(adding/deleting elements to a file)

  10. Pingback: more on struct.

  11. Pingback: Do I use struct correctly?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s