4

I have the following program to take five user entered names and print them out.

I need to ask for each name one by one, then prompt the user to either print the list of names or add another name to the list. The names must be stored in a two dimensional array, though I don't see why it couldn't be done with a regular array.

My code accepts the names with no issues, but fails to print anything. It includes print tests to monitor where the error happens. Test number 6 does not print, so there must be an issue with printf("Name: %s", names[x][y]);

What is the error?

#include <stdio.h>

    int main() {
    int x;
    int y;
    char names[5][51] = {{'\0'},{'\0'}};

    printf("Enter the names: ");
    for (x = 0; x <5; x++) {
        printf("\nPrintTest 1");
        for (y = 0; y < 1; y++) {
        printf("\nPrintTest 2");
            scanf("%50s",&names[x][y]);
        }
    }
    printf("\nPrintTest 3");

    for (x = 0; x < 5; x++) {
        printf("\nPrintTest 4");
        for (y = 0; y < 1; y++) {
            printf("\nPrintTest 5");
            printf("Name: %s", names[x][y]);
            printf("\nPrintTest 6");
        }
    }
}
0

2 Answers 2

4

So here's my analysis

Your Mistake:

In fact you declared a 2D array where each xth index is basically the char*.

  • If you use names[x], it will get the char* pointer at the xth element of names.
  • If you use names[x][y] it will get the pointer at the xth element and then will access it's yth element which is a character and a character datatype is printed by %c not by %s.

Possible Solution:

If you want to print the arrays character by character, then you need to iterate the inner loop over the size of array which is 51 in your case, and then you can print the array by using %c instead of %s.

Or you can print the whole array using %s but in that case inner loop is not required because you will be printing the whole array at a time.

Updated Code:

Method # 01:

//Iterating over all the char*
for (x = 0; x < 5; x++)
{
        printf("\nPrintTest 4");

        //Use of inner loop - Printing the arrays character by character
        for (y = 0; y < 51; y++)
        {
            printf("\nPrintTest 5");
            printf("Name: %c", names[x][y]);
            printf("\nPrintTest 6");
        }
}

Method # 02:

//Iterating over all the char*
for (x = 0; x < 5; x++)
{
        printf("\nPrintTest 4");

        //Printing the arrays without the loop
        printf("\nPrintTest 5");
        printf("Name: %s", names[x]);
        printf("\nPrintTest 6");
}

Hope its clear now.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the clear explanation as well as demonstrations of two methods.
You are most welcome. I am glad you solved your problem :)
3

You do not need the nested loop on y:

char names[5][51];
printf("Enter the names: ");
for (int x = 0; x <5; x++) {
    printf("\nPrintTest 1");
    scanf("%50s", names[x]);
    printf("\nPrintTest 2");
}
printf("\nPrintTest 3");
for (int x = 0; x < 5; x++) {
    printf("\nPrintTest 4");
    printf("Name: %s\n", names[x]);
    printf("\nPrintTest 5");
}
printf("\nDone.\n");

Demo.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.