Nick M Nick M - 8 months ago 51
C Question

Unexpected Output using Struct Array in C

So I have a struct, and within that struct I have a array of chars that is meant to store a phone number. Format being xxxxxxxxxx (with x being a digit from 0-9). Then, I'm running a for loop to format the display of those ten digits while printing.

#include <stdio.h>

void clear(void);

struct Student {
int idNumber;
char firstName[25];
char lastName[25];
char cellNumber[15];
char grade, grade2;
char semester[11], semester2[11];
char course[10], course2[10];

int main() {

struct Student studentArr[2];

for (int x = 0; x < 2; x++){

printf("For student %d...\n", x+1);
printf("Enter student number: \n");
scanf("%d", &studentArr[x].idNumber);
printf("%d", studentArr[x].idNumber);
printf("Enter student's first name: \n");
scanf("%s", studentArr[x].firstName);
printf("%s", studentArr[x].firstName);
printf("Enter student's last name: \n");
scanf("%s", studentArr[x].lastName);
printf("%s", studentArr[x].lastName);
printf("Enter student's cell number: \n");
scanf("%s", studentArr[x].cellNumber);
printf("%c", &studentArr[x].cellNumber[1]);
//print("%s", studentArr[x].cellNumber);
for(int i = 0; i < 3; i++) {
printf("%s", &studentArr[x].cellNumber[i]);}
for(int i = 3; i < 6; i++){
printf("%s", &studentArr[x].cellNumber[i]);}
for(int i = 6; i < 10; i++){
printf("%s", &studentArr[x].cellNumber[i]);}
printf("Enter course %d: \n", x+1);
scanf("%s", studentArr[x].course);
printf("%s", studentArr[x].course);
printf("Enter semster for course %d: \n", x+1);
scanf("%s", studentArr[x].semester);
printf("%s", studentArr[x].semester);
printf("Enter letter grade for course %d: \n", x+1);
scanf("%s", studentArr[x].grade);
printf("%s", studentArr[x].grade);



void clear(void) {

while(getchar() != '\n');

Here is the output:

As you can see, the output for the phone number is completely messed up, also I'm ending up with a segmentation fault. Any help or advice is completely appreciated.

FWIW, I'm a novice so don't be too harsh :)

EDIT: Switched the %s to %c and now am getting this output and not prompt for user entry:
Letters instead of numbers

Answer Source

I understand being a new user. You should, however, review StackOverflow's How to ask? guidelines to understand what you should do prior to posting a question on StackOverflow. However, in the interest of helping out a new user of SO:

You should ensure your compiler warnings are on. If you didn't get warnings, then you should ensure you're using a good compiler like Clang.

When I compiled your code, I got these errors:

$cc phone.c -o phone
phone.c:34:22: warning: format specifies type 'int' but the argument has type 'char *' [-Wformat]
        printf("%c", &studentArr[x].cellNumber[1]);
                ~~   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
phone.c:52:21: warning: format specifies type 'char *' but the argument has type 'int' [-Wformat]
        scanf("%s", studentArr[x].grade);
               ~~   ^~~~~~~~~~~~~~~~~~~
phone.c:53:22: warning: format specifies type 'char *' but the argument has type 'char' [-Wformat]
        printf("%s", studentArr[x].grade);
                ~~   ^~~~~~~~~~~~~~~~~~~
3 warnings generated.

These clue you in to what mistakes you may have made.

In this case, you got a little confused between characters and strings, along with some other issues.

  1. Fixing your character/string issues:

    You made the error of passing a pointer to a character where you should've just passed the char itself. To fix this, you should delete the ampersand, changing this:

    printf("%c", &studentArr[x].cellNumber[i]);

    to this:

    printf("%c", studentArr[x].cellNumber[i]);

    for each of your three lines.

  2. You also probably want to print a '1' first, not the index 1 of the phone string (otherwise input 5555555555 outputs 5(555)555-5555).

    So, change this:

    printf("%c", &studentArr[x].cellNumber[1]);

    to this:

    printf("%c", '1')

    After you do #1 and #2, you will get output like this:

    gregEnter student's last name: 
    schmitEnter student's cell number: 
    1(630)608-9561Enter course 1:
  3. There are still errors in your code. From here, you should compile and inspect warnings, and then search for the errors you get, as they are well documented.