HOW to write program that scan string of strings with pointers

1

I'm trying to write a program that asks from the user to enter couple of names (in this case 3, check out my define), the program, with functions scan_names and print_names would scan the names and print them, no matter what I do I don't succeed with it :(

This is the exception I get: "Exception thrown at 0x0FD6FB7C (ucrtbased.dll) in Magshimim_EX175.exe: 0xC0000005: Access violation reading location 0x00616161."

#include <stdio.h>

#define LINE 3
#define LENGH 10

void print_names(char* names[LENGH], int line)
{
    printf("\nYour names are:\n");
    for (size_t i = 0; i < line; i++) {
        puts(names[i]);
    }
}

void scan_names(char* names[LENGH], int line)
{
    for (int i = 0; i < line; i++) {
        printf("\nEnter name %d:  ", i + 1);
        fgets(names[i],LENGH,stdin);
    }
}

int main(void)
{
    char names[LINE][LENGH] = { NULL };
    scan_names(names, LINE);
    print_names(names, LINE);
}
c
asked on Stack Overflow May 9, 2019 by shalom shalom • edited May 9, 2019 by shalom shalom

1 Answer

2

char names[LINE][LENGH] is a 2D array of characters. char* names[LENGH] is a 1D array of character pointers. Like your compiler is telling you if you bother reading the warnings/errors: the types are not compatible.

Change the functions to void print_names(char names[LINE][LENGH], int line).

answered on Stack Overflow May 9, 2019 by Lundin

User contributions licensed under CC BY-SA 3.0