Likasumboode Likasumboode - 1 month ago 5
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.
char
not
int
. The issue is, the character string that is assigned to
flightNumber[MAX]
needs to change depending on what the user inputs for their choice above.

Relevant code from
main()
is pasted below

flightMenu();
scanf( "%d", &choice );
while ( choice < 1 || choice > 3 ) {
printf( "That is not a valid flight option!" );
flightMenu();
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
strcpy
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

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");
   gets(source);

   strcpy(destination, source);

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

   return 0;
}

http://www.programmingsimplified.com/c/source-code/c-program-copy-strings

Comments