Jump to content
  • Advertisement
Sign in to follow this  
JavaMava

Bah! newb question, but how do I make a string variable in C?

This topic is 4092 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've looked through the lessons on my professors site, and it talks about variables, but no example of actually making a string. Basically I want this s string_name = "This is the string."; but whats the proper syntax, in C?

Share this post


Link to post
Share on other sites
Advertisement
C uses pointers:

char string[] = "mystring";

char* string = "mystring";

In the above, string[0] points to the first character in the string ('m').

Share this post


Link to post
Share on other sites
If you don't mind using C++ then you can use the much friendlier version...


#include <string> // need this header

std::string s = "This is the string.";


Share this post


Link to post
Share on other sites
Quote:
Original post by JavaMava
s string_name = "This is the string.";

but whats the proper syntax, in C?


char[] string_name = "This is the string.";

There is no string type in C, you have to use pointers to string literals. You also have to have seperate arrays to do any kind of string manipulation which can cause buffer overflow issues.

Dealing with text in C is not pretty and is downright ineffecient since it has to constantly count characters.

Share this post


Link to post
Share on other sites
Thanks every one for the help. Worked like a charm.

I'm using C since it's my first year in college and thats what they want us to use. Don't get to touch C++ until second year(third semester).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!