wingkchan
asked on
C Programming, assigning strings to char
Hi All,
I had once learned some programming years ago, but almost can't remember how to program anymore. And now I am trying to pick up c programming again from the begining... and are stuck on how to assigning text to a variable.
Can anyone tell me why I get an error when I run something like the following in a program? Thanks.
warning: In function ‘main’:
test1.c:57: warning: assignment makes integer from pointer without a cast
void main()
{
int m;
char mon;
m=1;
if(m==1)
{
mon="January";
}
printf("Month is: \n",mon);
}
I had once learned some programming years ago, but almost can't remember how to program anymore. And now I am trying to pick up c programming again from the begining... and are stuck on how to assigning text to a variable.
Can anyone tell me why I get an error when I run something like the following in a program? Thanks.
warning: In function ‘main’:
test1.c:57: warning: assignment makes integer from pointer without a cast
void main()
{
int m;
char mon;
m=1;
if(m==1)
{
mon="January";
}
printf("Month is: \n",mon);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
"January" is an array of characters and C compiler holds it as a pointer but mon is a character.
In this assinment Compiler tries to convert a pointer to a character so it shows a warning.
you can use below code:
In this assinment Compiler tries to convert a pointer to a character so it shows a warning.
you can use below code:
char mon[100] = "September";
....
if (m == 1)
{
strcpy (mon, "January");
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> test1.c:57: error: incompatible types when assigning to type ‘char[16]’ from type ‘char *’
String assignment doesn't exist in C. You cannot assign a string to another.
What you can do, is assign a pointer to a string to another pointer to a string. Like in the example code I posted in my previous post.
>> 45: if(sscanf(argv[2],"%d", &m)==1)
sscanf returns the amount of succesfully read tokens. So, if it returns 1 in this case, it means the sscanf succeeded (because it needed to read one %d token, and it did so). So, it's good to check for success in this way.
However, you will still have to check the actual value in m to know which month it is.
In any case :
>> 15: char mon,
if you change this to be a const char* instead of a char (see the code snippet in my earlier post), it should work better.
Note also that there's a comma at the end of that line, while it should be a semicolon.
String assignment doesn't exist in C. You cannot assign a string to another.
What you can do, is assign a pointer to a string to another pointer to a string. Like in the example code I posted in my previous post.
>> 45: if(sscanf(argv[2],"%d", &m)==1)
sscanf returns the amount of succesfully read tokens. So, if it returns 1 in this case, it means the sscanf succeeded (because it needed to read one %d token, and it did so). So, it's good to check for success in this way.
However, you will still have to check the actual value in m to know which month it is.
In any case :
>> 15: char mon,
if you change this to be a const char* instead of a char (see the code snippet in my earlier post), it should work better.
Note also that there's a comma at the end of that line, while it should be a semicolon.
ASKER
Thanks all for your input and comments. especially for the breakdown descriptions. Actually, i am still unfamiliar with pointers, char syntax.... but nevertheness, with infinity08's code, I did get pass with matching the mon(integer) to text for now... let me keep on writing it. will return back shortly. thanks.
ASKER
Thanks again for the input. Storing the words in pointer solved the problem! =)
ASKER
test1.c: In function ‘main’:
test1.c:57: error: incompatible types when assigning to type ‘char[16]’ from type ‘char *’
did i do it wrong?
If you don't mind, let me share my codes with you... it's an assignment of converting the 3 number arguments (year month day), and then outputs them in american date format. I had worked on it for a day... although i know the logic and programming flow is very beginner like ...
Open in new window