0

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

1
  • 1
    You do know r[0].burger_type = "chicken Burger"; would be the first element in the array? (arrays are zero based in C) You also know you are not storing "chicken Burger" in the array and you need to use strcpy() to copy the string. Commented May 6, 2021 at 4:21

1 Answer 1

2

== 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 ...).

Sign up to request clarification or add additional context in comments.

2 Comments

Do note that there's a case where strncpy will not add the string null-terminator. When using strncpy always make sure to add it.
Best to use strcpy() instead (especially if the destination array is large)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.