0

i am not getting output for this program. // Pointer and structure I want get output as 1Jatin. but for refreshing pointer by using structure. i cant do that. It working as int and float. but not as int and char. any one please solved out this.

#include <stdio.h>

struct name{
    int a;
    char b;
};

int main(){
    struct name *ptr ,p;
    ptr = &p;
    printf("Enter integer:");
    scanf("%d", &(*ptr).a);
    printf("Enter name:");
    scanf("%s", &(*ptr).b);
    printf("Displaying:");
    printf("%d%s",(*ptr).a,(*ptr).b);

    return 0;
}
Jatin Roy
  • 209
  • 2
  • 7

1 Answers1

2
#include <stdio.h>

#define MAX_NAME_LENGTH 32

struct name{
    int a;
    char b[MAX_NAME_LENGTH];
};

int main(){
    struct name *ptr ,p;
    ptr = &p;
    printf("Enter integer:");
    scanf("%d", &ptr->a);
    printf("Enter name:");
    scanf("%s", ptr->b);
    printf("Displaying: ");
    printf("%d %s\n",ptr->a,ptr->b);

    return 0;
}

Many things:

  1. You can simply us -> operator to dereference pointer to its members
  2. C-Strings are null terminated arrays of chars. That means that b member must be an array large enough to store a name characters + null terminator ('\0', 0x00, 0).
Community
  • 1
  • 1
LPs
  • 16,045
  • 8
  • 30
  • 61