Store the same string in two different struct types

array within structure in c
array of structure in c
how to assign a string to a structure variable in c
how to link two structures in c
string in structure in c
structure assignment in c
pointer to structure array
structure in c example

I'm working on structures, and I need to assign a string in a char array. Here is my some spesific parts of my code:

typedef struct{
    int std_id;
    char std_name[STD_NAME_SIZE];
    double gpa;
    char courses[COURSE_SIZE];

typedef struct{
    int course_id;
    char course_name[COURSE_NAME_SIZE];
    char course_code[COURSE_CODE_SIZE];
    char course_acronym[COURSE_ACRONYM_SIZE];
    int quota;

this is the struct that I use. and this is the assignment part:

for(int i = 0; i < COURSE_SIZE; i++){
        printf("Enter course name: ");
        scanf("%s", &courseDetails.course_name);[i] = courseDetails.course_name;


    for(int j = 0; j < COURSE_SIZE; j++){
        printf("\nYour %d course is: %s", j + 1,[j]);

I think the problem happens at[i] = courseDetails.course_name part. Should I use pointers? How can I solve this problem ?

You need to make the courses field an array of pointers to char. Also, courseDetails needs to be an array:

#include <stdio.h>
#include <stdlib.h>

#define STD_NAME_SIZE       81
#define COURSE_SIZE         3
#define COURSE_NAME_SIZE    81
#define COURSE_CODE_SIZE    81

typedef struct {
    int     std_id;
    char    std_name[STD_NAME_SIZE];
    double  gpa;
    char    *courses[COURSE_SIZE]; // Make this an array of pointers
} student_t;

typedef struct{
    int     course_id;
    char    course_name[COURSE_NAME_SIZE];
    char    course_code[COURSE_CODE_SIZE];
    char    course_acronym[COURSE_ACRONYM_SIZE];
    int     quota;
} course_t;

int main(void)
    student_t   studentProfile;
    course_t    courseDetails[COURSE_SIZE];

    for (int i = 0; i < COURSE_SIZE; i++){
        printf("Enter course name: ");
        scanf("%s", courseDetails[i].course_name);[i] = courseDetails[i].course_name;

    for (int j = 0; j < COURSE_SIZE; j++){
        printf("Your %d course is: %s\n", j + 1,[j]);

    return 0;

Compiled With

gcc -Wall -Werror -o scratch scratch.c

Example Input and Output

Enter course name: first
Enter course name: second
Enter course name: third

Your 1 course is: first
Your 2 course is: second
Your 3 course is: third


This approach is very fragile because the string pointers in studentProfile point to strings stored in other variables. I understand that this is a toy program that is part of some kind of programming homework. However, I just want to caution anyone against using this code in a production environment. In a real program, you would probably just store an array of course_id values in the student_t. To get the course name, you would first find the course_t with the matching course_id and then display the course_name.

Structures in C, We use structures to store data of different types. We can also declare structure variables at the time of defining structure as follows The reason for this is that in C, we cannot equate two strings (i.e. character It is same as we were using (.)� Instances of a ref struct type are allocated on the stack and can't escape to the managed heap. To ensure that, the compiler limits the usage of ref struct types as follows: A ref struct can't be the element type of an array. A ref struct can't be a declared type of a field of a class or a non-ref struct. A ref struct can't implement interfaces.

Presumably, COURSE_SIZE is defined as the maximum number of courses that a student can take. Then in the student_t struct, courses would be declared as char courses[COURSE_SIZE][COURSE_NAME_SIZE] and it would be set as strcpy([i], courseDetails.course_name); But that is still not the correct way to do it. What you really want is the courses array to be an array of courses:

course_t *courses[COURSE_SIZE];

and set courses[i] to point to some course j:[i] = &courseDetails[j]; 

then if you want to refer to the name of the ith course taken by that student you would use courses[i]->course_name.

Note that you will have to declare the course_t struct before you declare the student_t struct.

C/Structs, Structs. A struct is a way to define a type that consists of one or more other types pasted When you declare a variable with type struct string, the compiler allocates 1 struct string { 2 int length; 3 char *data; 4 }; 5 6 int 7 main(int argc, char to store the largest one, and makes all the components refer to the same address. That is, data of multiple types. Well MATLAB, like many other languages, provides ways to do that. The easiest way is to use a struct. A struct is different from an array in three ways. One, structs have fields, not elements. Two, we access the fields of a struct by names, not indices. And three, fields within the same struct can have different

Since you're using not using pointers, you should use something like

strcpy([i], courseDetails.course_name);

EDIT: this is obviously wrong, as comment points out.

maybe you just forgot array access for the other variable, as in


Array of Structures in C, Since an array is a collection of elements of the same type. In a… We can use arr_car to store 10 structure variables of type struct car . #include<stdio.h> # include<string.h> #define MAX 2 struct student { char name[20]; int� Rust struct &str and String. You may be wondering why we need to_string(). Rust has two main types of strings: &str and String. &str is called ‘string slices’. A string slice has a fixed size, and cannot be mutated. A String is stored as a vector. String is heap-allocated, growable, and not null

Structures in Go (structs). Unlike traditional Object Oriented…, A structure has different fields of the same or different data types. Hence string will have zero value of "" (can't be printed), int will have zero Hence, we can store an instance of Salary struct type in a field of Salaried type. In the above struct type Employee , FirstName and LastName are the only two� Define a structure with multiple data types and then use an Array of that struct. Example: [code]struct data { int a; Char ch; float f;}; data d[10]; //Array of size 10, type data [/code]

Structures - MATLAB & Simulink, To concatenate structures, they must have the same set of fields, but the fields Both cell and structure arrays allow you to store data of different types and sizes. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

C Programming Course Notes, Even if other struct variables are created with the same data types and field names much the same way as arrays, by enclosing multiple data fields in curly braces. stored, unions are often nested inside of structs, with an enumerated type� In C language, Structures provide a method for packing together data of different types. A Structure is a helpful tool to handle a group of logically related data items. However, C structures have some limitations. The C structure does not allow the struct data type to be treated like built-in data types: