-3

Getting a buffer overflow when compiling this code. Can anyone spot the issue? No error at all when compiled in Windows terminal but the strings refuse to print. Tried changing to subscript notation but compiler just throwing loads of errors.

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

// #define's
#define PEOPLE 3
#define LINES 4
#define NAMELENGTH 30
#define ARRAYSIZE 24

// structure templates
struct certs
{
    int 
        employee_ID1, 
        employee_ID2, 
        employee_ID3, 
        employee_ID4, 
        employee_ID5, 
        employee_ID6;
    
    char 
        *certificate1, 
        *certificate2, 
        *certificate3, 
        *certificate4, 
        *certificate5, 
        *certificate6;
};

struct details
{
    char    *firstName1[NAMELENGTH], 
            *firstName2[NAMELENGTH], 
            *firstName3[NAMELENGTH], 
            *firstName4[NAMELENGTH], 
            *firstName5[NAMELENGTH], 
            *firstName6[NAMELENGTH];
    
    char    *surName1[NAMELENGTH], 
            *surName2[NAMELENGTH], 
            *surName3[NAMELENGTH], 
            *surName4[NAMELENGTH], 
            *surName5[NAMELENGTH], 
            *surName6[NAMELENGTH];
    
    struct certs ID_cert;
};

struct line
{
    struct details line1;
    struct details line2;
    struct details line3;
    struct details line4;
};

int main()
{
    int i, j;
    struct line lineDetails;
    

removed the other strcpy's for the purposes of the question.

    // SurNames (Team 4)
    strcpy(lineDetails.line4.surName1[NAMELENGTH], "powers");
    strcpy(lineDetails.line4.surName2[NAMELENGTH], "garcia");
    strcpy(lineDetails.line4.surName3[NAMELENGTH], "smith");
    strcpy(lineDetails.line4.surName4[NAMELENGTH], "woods");
    strcpy(lineDetails.line4.surName5[NAMELENGTH], "wick");
    strcpy(lineDetails.line4.surName6[NAMELENGTH], "collins");
    

  
    printf("\n%s",lineDetails.line4.surName2);


}
Darth-CodeX
  • 2,166
  • 1
  • 6
  • 23
danocar
  • 1
  • 2
  • 1
    Please provide a [mre] of the problem. If you removed stuff from your code because you considered it unimportant, then please verify that the truncated code still compiles and reproduces the problem. If it does, then please post this truncated code as is, in one code snippet. That way, we can simply copy&paste the code, in order to test it. Please don't post the code as several code snippets, unless you have a valid reason to do this. – Andreas Wenzel Apr 14 '22 at 13:19
  • 1
    `char *firstName1[NAMELENGTH]` declares `firstName1` and an array of length `NAMELENGTH` of pointers to `char`. I really doubt that any of that is what you're intending. I don't think any of those should be pointers. – Thomas Jager Apr 14 '22 at 13:20
  • This: `char *firstName1[NAMELENGTH],` should be `char firstName1[NAMELENGTH],` to create a buffer. (You do not need a pointer to array here.) And this: `strcpy(lineDetails.line4.surName1[NAMELENGTH], "powers");` should be `strcpy(lineDetails.line4.surName1, "powers");` i.e. `[NAMELENGTH]` is used during declaration to size the buffer. – ryyker Apr 14 '22 at 13:21
  • You need to study arrays before studying pointers and strings. Some ~50% of all C beginner questions on SO seems to be related to not reading the array chapter before the pointer chapter before the C chapter in the beginner-level C book. It's mighty strange, since these chapters in every C book I've ever read come in that order... – Lundin Apr 14 '22 at 13:30

1 Answers1

1

lineDetails.line4.surName1 has only NAMELENGTH elements, so the available indice is only 0 to NAMELENGTH-1. Therefore, lineDetails.line4.surName1[NAMELENGTH] is out-of-range access.

It looks like you wanted to allocate arrays of characters, not arrays of pointers to characters.

Instead of this type of declaration:

char    *surName1[NAMELENGTH]

You should declare arrays of characters:

char    surName1[NAMELENGTH]

And copy strings to the array:

strcpy(lineDetails.line4.surName1, "powers");

instead of:

strcpy(lineDetails.line4.surName1[NAMELENGTH], "powers");
MikeCAT
  • 73,922
  • 11
  • 45
  • 70