Likasumboode Likasumboode - 1 year ago 84
C Question

gcc error when writing string to char array

When attempting to assign a character string to the previously defined

char flightNumber[MAX]
, I'm hit with
error: assignment makes integer from pointer without a cast [-Werror]

which is strange to me because I'm not using pointers, and I believe I am using the proper cast type, i.e.
. The issue is, the character string that is assigned to
needs to change depending on what the user inputs for their choice above.

Relevant code from
is pasted below

scanf( "%d", &choice );
while ( choice < 1 || choice > 3 ) {
printf( "That is not a valid flight option!" );
scanf( "%d", &choice );

char name[MAX];
char flightNumber[MAX];

printf( "Enter your first name: ");
scanf( "%s", &name[MAX] );

if ( choice == 1 ) {
flightNumber[MAX] = "MIA1050";
seatReservation( flight1 );

else if ( choice == 2 ) {
flightNumber[MAX] = "BNA1050";
seatReservation( flight2 );
else if ( choice == 3 ) {
flightNumber[MAX] = "LAS1050";
seatReservation( flight3 );

Looks like other people have had similar issues, but I don't know how to use things like
or pointers. Apologies for noob question and poor formatting, but nothing I do seems to fix the error, and my guess is I'm overlooking a really simple problem...

Answer Source

To copy a string you should use special function like:

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

int main()
   char source[1000], destination[1000];

   printf("Input a string\n");

   strcpy(destination, source);

   printf("Source string:      \"%s\"\n", source);
   printf("Destination string: \"%s\"\n", destination);

   return 0;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download