• C

passing a constant string to a char variable

hello! im new on using c and my question is how do i pass a string to a char variable ? how to i declare it? sort of like this:

var1 = 'anna'
printf("%c ", var1)

i need to pass 'anna' to a variable. can u give me an example? pls? thanks!
LJ GaviolaSoftware QAAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

VileCommented:
the most basic way is to declare var1 as a char array and put the string in it. you can then use printf with the conversion specifier %s like this

char var1[10];

var1 = "anna";

printf("%s", var1);

you can also access the individual characters in the char array by their indices...
var1[0] == 'a'
var1[1] == 'n'
....
var1[3] == 'a'
var1[4] == '\0'
0
LJ GaviolaSoftware QAAuthor Commented:
ok, here's the code that i used:

char var1[20];

if (y == 1940)
{
var1[20] = "dragon";
printf("zodiac is: %s", var1);
}

this doesn't work.. i got this warning message:

"warning C4047: '=' : 'char ' differs in levels of indirection from 'char [7]'"
0
abdijCommented:
Hi,
 You cannot pass a String to a char variable. It has to be an array of characters as Vile said. Or there is the pointer concept

char* pStr = "Hi this is an example";
printf("The String is %s\n",pStr);

For further C reference you can use the URL
1. http://www.eskimo.com/~scs/cclass/notes/sx8.html
2. http://www.strath.ac.uk/CC/Courses/NewCcourse/ccourse.html

Bye
Abdij
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Increase Security & Decrease Risk with NSPM Tools

Analyst firm, Enterprise Management Associates (EMA) reveals significant benefits to enterprises when using Network Security Policy Management (NSPM) solutions, while organizations without, experienced issues including non standard security policies and failed cloud migrations

abdijCommented:
Hi,
 I was just a fraction of second late i suppose.
Regarding what you have done, the error is valid.

Strings are not intrinsic basic data types in C. They are derived from char.
So you have to use String manipulation function to achieve string operations.

For example
1. strcpy() to copy strings
2. strcmp() to compare strings.
etc.

I have given URLs to C programming sites hope that will be helpful.

Feel free to ask,

Bye
Abdij
0
ozoCommented:
char *var1;

if (y == 1940)
{
var1 = "dragon";
printf("zodiac is: %s", var1);
}
0
abdijCommented:
Hi,
 Ozo: I think this has already been told!!!!!

Bye
Abdij
0
VileCommented:
oooops... guess I should have double-checked myself. if var1 is declared as an array the only way to make the assignment is in the declaration
char var1[10] = "anna";
otherwise abdij is right... he deserves the points.. not my dumb self. :)
0
LJ GaviolaSoftware QAAuthor Commented:
thanks! im awarding the points to you abdij's. i'm sure i'll be asking questions again so have patience with me :)
0
abdijCommented:
Hi,
 You are welcome
Feel free to ask
Bye
Abdij
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.