?
Solved

passing a constant string to a char variable

Posted on 2000-01-26
9
Medium Priority
?
298 Views
Last Modified: 2010-08-05
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!
0
Comment
Question by:LJ Gaviola
  • 4
  • 2
  • 2
  • +1
9 Comments
 

Expert Comment

by:Vile
ID: 2391732
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
 

Author Comment

by:LJ Gaviola
ID: 2391750
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
 
LVL 2

Accepted Solution

by:
abdij earned 120 total points
ID: 2391757
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
Will You Be GDPR Compliant by 5/28/2018?

GDPR? That's a regulation for the European Union. But, if you collect data from customers or employees within the EU, then you need to know about GDPR and make sure your organization is compliant by May 2018. Check out our preparation checklist to make sure you're on track today!

 
LVL 2

Expert Comment

by:abdij
ID: 2391764
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
 
LVL 85

Expert Comment

by:ozo
ID: 2391952
char *var1;

if (y == 1940)
{
var1 = "dragon";
printf("zodiac is: %s", var1);
}
0
 
LVL 2

Expert Comment

by:abdij
ID: 2391991
Hi,
 Ozo: I think this has already been told!!!!!

Bye
Abdij
0
 

Expert Comment

by:Vile
ID: 2393543
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
 

Author Comment

by:LJ Gaviola
ID: 2395193
thanks! im awarding the points to you abdij's. i'm sure i'll be asking questions again so have patience with me :)
0
 
LVL 2

Expert Comment

by:abdij
ID: 2395742
Hi,
 You are welcome
Feel free to ask
Bye
Abdij
0

Featured Post

Free tool for managing users' photos in Office 365

Easily upload multiple users’ photos to Office 365. Manage them with an intuitive GUI and use handy built-in cropping and resizing options. Link photos with users based on Azure AD attributes. Free tool!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
There's never been a better time to become a computer scientist. Employment growth in the field is expected to reach 22% overall by 2020, and if you want to get in on the action, it’s a good idea to think about at least minoring in computer science …
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

594 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question