-3
#include <stdio.h>
#include <stdarg.h>
#define ammo "full"

int main()
{
  char a[100];
  a = ammo;
  printf("%s",a);
  return 0;
}

I am trying to replace #define ammo with "full" (string) and want to print it on the screen to check if it works but the code is not compiling.

Lukas
  • 1,320
  • 12
  • 20

2 Answers2

2

Instead of a=ammo; you should use strcpy(a,ammo); you should not use a direct assignment when you have strings, but use the C method strcpy to copy a string to another. it will work

Math Lover
  • 148
  • 10
2

It's invalid to assign a C-string to an array (except when initializing, see below). You must "copy" the string to the array because the space is already allocated:

strcpy(a, ammo);

Better yet, use a safer version of copy function:

strncpy(a, ammo, sizeof(a) / sizeof(char));

Or directly assign it when initializing:

char a[100] = ammo;

Or, don't use array. Use a pointer instead:

char *a;
a = ammo;

Note you can't change the content of the string if you use a pointer.

iBug
  • 35,554
  • 7
  • 89
  • 134