Skip to main content

C Program to Store Data in Structures Dynamically

 

Demonstrate the Dynamic Memory Allocation for Structure

#include <stdio.h>
#include <stdlib.h>
struct course {
    int marks;
    char subject[30];
};

int main() {
    struct course *ptr;
    int i, noOfRecords;
    printf("Enter the number of records: ");
    scanf("%d", &noOfRecords);

    // Memory allocation for noOfRecords structures
    ptr = (struct course *)malloc(noOfRecords * sizeof(struct course));
    for (i = 0; i < noOfRecords; ++i) {
        printf("Enter the name of the subject and marks respectively:\n");
        scanf("%s %d", (ptr + i)->subject, &(ptr + i)->marks);
    }

    printf("Displaying Information:\n");
    for (i = 0; i < noOfRecords; ++i)
        printf("%s\t%d\n", (ptr + i)->subject, (ptr + i)->marks);

    return 0;
}

Output

Enter the number of records: 2
Enter the name of the subject and marks respectively:
Programming
22
Enter the name of the subject and marks respectively:
Structure
33

Displaying Information:
Programming      22
Structure        33

Comments

Popular posts from this blog

C Program to Calculate Difference Between Two Time Periods

  Calculate Difference Between Two Time Periods # include <stdio.h> struct TIME { int seconds; int minutes; int hours; }; void differenceBetweenTimePeriod (struct TIME t1, struct TIME t2, struct TIME *diff) ; int main () { struct TIME startTime , stopTime , diff ; printf ( "Enter the start time. \n" ); printf ( "Enter hours, minutes and seconds: " ); scanf ( "%d %d %d" , &startTime.hours, &startTime.minutes, &startTime.seconds); printf ( "Enter the stop time. \n" ); printf ( "Enter hours, minutes and seconds: " ); scanf ( "%d %d %d" , &stopTime.hours, &stopTime.minutes, &stopTime.seconds); // Difference between start and stop time differenceBetweenTimePeriod(startTime, stopTime, &diff); printf ( "\nTime Difference: %d:%d:%d - ...

C Program to Read a Line From a File and Display it

  Program to read text from a file # include <stdio.h> # include <stdlib.h> // For exit() function int main () { char c[ 1000 ]; FILE *fptr; if ((fptr = fopen( "program.txt" , "r" )) == NULL ) { printf ( "Error! opening file" ); // Program exits if file pointer returns NULL. exit ( 1 ); } // reads text until newline is encountered fscanf (fptr, "%[^\n]" , c); printf ( "Data from the file:\n%s" , c); fclose(fptr); return 0 ; } If the file is found, the program saves the content of the file to a string  c  until  '\n'  newline is encountered. Suppose the  program.txt  file contains the following text in the current directory. C programming is awesome. I love C programming. How are you doing? The output of the program will be: Data from the file: C programming is awesome. If the file  program.txt  is not found, this program prints an error message.

C Program to Sort Elements in Lexicographical Order (Dictionary Order)

  Sort strings in the dictionary order # include <stdio.h> # include <string.h> int main () { char str[ 5 ][ 50 ], temp[ 50 ]; printf ( "Enter 5 words: " ); // Getting strings input for ( int i = 0 ; i < 5 ; ++i) { fgets(str[i], sizeof (str[i]), stdin ); } // storing strings in the lexicographical order for ( int i = 0 ; i < 5 ; ++i) { for ( int j = i + 1 ; j < 5 ; ++j) { // swapping strings if they are not in the lexicographical order if ( strcmp (str[i], str[j]) > 0 ) { strcpy (temp, str[i]); strcpy (str[i], str[j]); strcpy (str[j], temp); } } } printf ( "\nIn the lexicographical order: \n" ); for ( int i = 0 ; i < 5 ; ++i) { fputs (str[i], stdout ); } return 0 ; } Output Enter 5 words: R programming JavaScript Java C programming C++ programming In the lexicographical order: C programming C+...