Nixey
Nixey

Reputation: 3

Having trouble storing string inside an array of C struct

Hey I'm simply trying to store a string inside of a struct so i can call on it later in printf's. For some reason it doesn't store the string or print it. What am i doing wrong?

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

struct receipt
{
  char burger_type[45];
};
struct receipt r[25];
int main()
{  
  r[1].burger_type == "chicken Burger";
  
  printf("%s pls work",r[1].burger_type);

  return 0;
}

Thank you too anyone who can help. Thanks

Upvotes: 0

Views: 88

Answers (1)

Oka
Oka

Reputation: 26345

== is the equality operator. For assignment you use the = assignment operator.

Regardless of that, you cannot assign a value of type const char * to a char [45]. Use strcpy to place the value of that string in your destination.

strcpy(r[1].burger_type, "chicken Burger");

When using these functions, you must be cautious that the size of your input does not exceed the size of your destination.

You should turn up the warnings on your compiler to catch these kinds of malformed code (e.g., gcc -Wall ...).

Upvotes: 2

Related Questions