Home > OS >  Why is struct pointer variable printing the whole thing inside the struct variable it's pointin
Why is struct pointer variable printing the whole thing inside the struct variable it's pointin

Time:01-21

I created a struct datatype 'ans' that contains three string datatype member variables a[2],b[2],c[2]. Inside main, I created a struct variable 'p' to accept the three string inputs and then pass it to a function - void f1(ans *x) via call by reference to print the strings. Now in the function, instead of printing the three separate strings (*x).a,(*x).b,(*x).c, it is printing the whole string joined together. I am attaching the code and output for reference:

#include <stdio.h>

typedef struct 
{
    char a[2];
    char b[2];
    char c[2];
} ans;

void f1(ans *x) {
   printf("The strings are :\n");
   printf("%s\n",(*x).a);
   printf("%s\n",(*x).b);
   printf("%s\n",(*x).c);
}

int main() {
    ans p;
    printf("Enter for a:\n");
    scanf("%s", p.a);
    printf("Enter for b:\n");
    scanf("%s", p.b);
    printf("Enter for c:\n");
    scanf("%s", p.c);

    f1(&p);

    return 0;
}

Sample output:

Enter for a:
ab
Enter for b:
cd
Enter for c:
ef
The strings are :
abcdef
cdef
ef

Can anyone explain why is this showing as output instead of the following:

The strings are:
ab
cd
ef

I can't figure out what's happening :(

CodePudding user response:

In scanf("%s", p.a);, scanf reads characters and writes them to the memory pointed to by p.a. It also writes a terminating null character after them. Since the a member of the structure is declared as char a[2];, when scanf writes more than two characters, including the terminating null, the behavior is not defined by the C standard.

In printf("%s\n",(*x).a);, for %s, printf takes a pointer to a char and prints the characters it finds there until a terminating null character marks the end of the string. When there is no terminating null character the array that is (*x).a, printf overruns the array, and the behavior is not defined by the C standard.

To fix the problem, ensure there is enough space in the arrays for all the characters to be written into them, including the terminating null character, or ensure that no more characters are written in the arrays than will fit.

CodePudding user response:

if you enter "ab", scanf will scan string as "ab\0" which is 3 characters, therefore your a[2] won't fit as same as other variabls.

CodePudding user response:

To prevent scanf from overruns the array, a simple adaptation of your program, from the several that can be suggested:

#define MAXCH   3

typedef struct 
{
    char a[MAXCH];
    char b[MAXCH];
    char c[MAXCH];
} ans;

void f1(ans *x)
{
   printf("The strings are :\n");
   printf("%s\n",(*x).a);
   printf("%s\n",(*x).b);
   printf("%s\n",(*x).c);
}

int main()
{
    ans p;
    char in[128];
    
    printf("Enter for a:\n");
    scanf("%s",in);
    snprintf(p.a,MAXCH,"%s",in);
    printf("Enter for b:\n");
    scanf("%s",in);
    snprintf(p.b,MAXCH,"%s",in);
    printf("Enter for c:\n");
    scanf("%s",in);
    snprintf(p.c,MAXCH,"%s",in);

    f1(&p);

    return 0;
}

Sample output:

Enter for a:
abcdefgh
Enter for b:
cdefghij
Enter for c:
efghijkl
The strings are :
ab
cd
ef
  •  Tags:  
  • Related