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

int main(int argc, char *argv[])
{
    // Check for command line args
    if (argc != 2)
    {
        printf("Usage: ./read infile\n");
        return 1;
    }

    // Create buffer to read into
    char *buffer = malloc(7);

    // Create array to store plate numbers
    char *plates[8][7];




    FILE *infile = fopen(argv[1], "r");

    int idx = 0;

    while (fread(buffer, 1, 7, infile) == 7)
    {
        // Replace '\n' with '\0'
        buffer[6] = '\0';

        // Save plate number in array
        strcpy(plates[idx], buffer);
        idx++;
    }

    for (int i = 0; i < 8; i++)
    {
        printf("%s\n", plates[i]);
    }

    free(buffer);
    
}

When I try to strcpy the strings, I got this error. I didn't understand what does it mean. I staticly declare an array:

char *plates[8][7]

So this means this is an string array that the strings have 7 letter each, am I right?

Thank you in advance <3

1 Answer 1

1

No, you are incorrect. If that's what you want then you need to define plates as char plates[8][7] you want 8 char arrays of length 7. With what you declared you have a pointer to 8 arrays of 7 chars. But that is also not the format strcpy expects. strcpy wants straight pointers, so you will want to cast the pointer type: strcpy((char *)plates[idx], buffer). This is just part of the fun of mixing arrays and pointers together.

1
  • If this answers your question, please accept the answer so that the question is marked answered. thank you
    – UpAndAdam
    Commented Oct 16, 2023 at 20:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .