Home > database >  Accepting string from user in an array of pointers in C us
Accepting string from user in an array of pointers in C us

Time:02-05

The code tries to get names of 3 students and their marks in 3 subjects and print total score as well as average of score of each student along with their names.

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

int main() {

    char *names[3];
    int test1[3],test2[3],test3[3];
    for(int i=0;i<3;i  )
    {
        scanf("%s",&(*(names i)));
        scanf("%d",&test1[i]);
        scanf("%d",&test2[i]);
        scanf("%d",&test3[i]);
    }
    int j=0;
    while(j<3){
        
        printf("%s",*(names j));
        printf(" %d ",test1[j] test2[j] test3[j]);
        printf("%.f\n",(test1[j] test2[j] test3[j])/3.0);
        j  ;   
    }
    return 0;
}

But when I run this code, I am not getting output of any of the printf's written in while loop . Can anyone help me please?

CodePudding user response:

The variable names is an array of pointers. But you never make these pointers actually point anywhere. Any attempt to dereference them will lead to undefined behavior.

Also the type of &names[i] (which is what &(*(names i)) really is) is char **, which is not what the scanf format %s expects. Mismatching format specifier and argument type also leads to undefined behavior.

What you should use is an array of arrays of characters:

char names[3][256];

and let the sub-arrays decays to pointers to their first element (the expression names[i] decays to &names[i][0] which have the type char *):

scanf("%5s", names[i]);

Note that I added a length specifier to avoid writing out of bounds of names[i].

Then print as a normal string:

printf("%s %d %f\n", names[j],
       test1[j]   test2[j]   test3[j],
       (test1[j]   test2[j]   test3[j]) / 3.0);

CodePudding user response:

For starters you included too many headers.

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

Actually only declarations from the header <stdio.h> are used in the program. So remove all other headers and leave only the header <stdio.h>

#include <stdio.h>

In this declaration

char *names[3];

there is declare an uninitialized array of pointers that have indeterminate values.

So this call of scanf

scanf("%s",&(*(names i)));

that should be written at least like

scanf("%s",*(names i));

because the expression &(*(names i)) has the type char ** but the function expects an argument of the type char * invokes undefined behavior.

To store strings you need to declare an array of character arrays as for example

char names[3][100];

But in any case your approach with declaring four arrays looks is not good.

It will be better to declare a structure as for example

enum { NumberOfStudents = 3, NumberOfMarks = 3, MaxNameLen = 100 };

struct Student
{
    char name[MaxNameLen];
    unsigned int marks[NumberOfMarks];
};

In this case you could declare only one array of three object of the type struct Student.

Here is a demonstration program

#include <stdio.h>

int main( void )
{
    enum { NumberOfStudents = 3, NumberOfMarks = 3, MaxNameLen = 100 };

    struct Student
    {
        char name[MaxNameLen];
        unsigned int marks[NumberOfMarks];
    };

    struct Student students[NumberOfStudents];

    printf( "Enter information about %d srudents.\n", NumberOfStudents );

    for (int i = 0; i < NumberOfStudents; i  )
    {
        printf( "Name of student #%d: ", i   1 );

        scanf( "           
  • Related