-1

I am trying to printf the values in the structs from 2 arrays but the output gets very wierd and I dont know why.

this is whats get outputed: c123 6545645 bvabc123 vabc123 abc132

this should be outpued: Johan 47 Car Volvo abc123

whats wrong?

struct person{
    char name;
    int age;
};
typedef struct person p;

struct vehicle{
    char type;
    char brand;
    char regn;
    char owner;
};
typedef struct vehicle v;

int main(){
    p owner[1];
    v Vehicle[1];

    for(int i=0; i<1;i++){
        printf("Name\n");
        scanf("%s",&owner[i].name);
        printf("Age\n");
        scanf("%d",&owner[i].age);
        printf("Type\n");
        scanf("%s",&Vehicle[i].type);
        printf("Brand\n");
        scanf("%s",&Vehicle[i].brand);
        printf("regn\n");
        scanf("%s",&Vehicle[i].regn);


    }

    for(int j=0; j<1; j++){
        printf("%s\n", &owner[j].name);
        printf("%d\n", &owner[j].age);
        printf("%s\n", &Vehicle[j].type);
        printf("%s\n", &Vehicle[j].brand);
        printf("%s\n", &Vehicle[j].regn);
    }

}
anon
  • 237
  • 2
  • 11

3 Answers3

1

A char in C literally means a single character. A char is just simply a signed 8 bit integer. So when you write char in code, you're telling C that you're storing a single 8 bit integer - nothing more than that. A string, on the other hand, consists of many characters, where each character is an 8 bit integer (ie. a char). In C, strings are stored as consecutive 8 bit integers in memory (an array), where the array's length is one more than the length of the string, and this extra element at the end of the array is a null terminator (a single byte which is just a 0). Since strings can be of any length, we need to know how long they are (ie. where the array in memory ends). C does this by having this null terminator at the end of the string.

In your code, when you write char in a struct definition, you are telling C to only store a single character. What you want is a char * (pointer to a char), which points to an array of chars, where the last element is a null terminator. C inserts this terminator for you if you use double quotes when assigning the string.

What you want is something like this:

typedef struct person {
  char *name;
  // ...
} p;

int main(int argc, char *argv[]) {
  p person;
  person.name = "The name";
  printf("%s\n", person.name); // Prints `The name`
}
GravityScore
  • 297
  • 3
  • 12
1

The problem is that a char is supposed to be a single character such as 'a', 'B', '1', ect. and so what you need is an array of them. Below I arbitrarily chose 32 as the lengths of the arrays

Note that I removed & from several places in your code because it is now using arrays rather than single characters:

#include <stdio.h>

struct person {
    char name[32];
    int age;
};
typedef struct person p;

struct vehicle {
    char type[32];
    char brand[32];
    char regn[32];
    char owner[32];
};
typedef struct vehicle v;

int main(){
    p owner[1];
    v Vehicle[1];

    for (int i = 0; i < 1; i++) {
        printf("Name\n");
        scanf("%s", owner[i].name);
        printf("Age\n");
        scanf("%d", &owner[i].age);
        printf("Type\n");
        scanf("%s", Vehicle[i].type);
        printf("Brand\n");
        scanf("%s", Vehicle[i].brand);
        printf("regn\n");
        scanf("%s", Vehicle[i].regn);
    }

    for (int j = 0; j < 1; j++) {
        printf("%s\n", owner[j].name);
        printf("%d\n", owner[j].age);
        printf("%s\n", Vehicle[j].type);
        printf("%s\n", Vehicle[j].brand);
        printf("%s\n", Vehicle[j].regn);
    }
}

Also note that this code will cause undefined behavior if the user inputs a string that overflows the storage space of 32 because it does not check for that. Additionally if the user inputs white space for any of the scanf that will cause problems. There is a solution to this if that is important to you here: How do you allow spaces to be entered using scanf?

Community
  • 1
  • 1
asimes
  • 5,749
  • 5
  • 39
  • 76
1

First,for name,type,brand and regn variables,they are strings,so you should use char array to define them.

Second,you shouldn't use & in scanf function,when the variable is array; Last,you shouldn't use & in printf function.

last,you shouldn't use & in printf function.

scanf("%s",owner[i].name);
printf("%s\n",owner[j].name);
Kei H
  • 55
  • 7