Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

strinfs

Posted on 1997-02-19
1
Medium Priority
?
276 Views
Last Modified: 2012-05-04
How do I work strings like in BASIC?
IE How do string's work in C?
0
Comment
Question by:Robert Mcnulty
1 Comment
 
LVL 2

Accepted Solution

by:
morleys earned 100 total points
ID: 1249382
Strings don't realy exist in c (although forms of them exist is c++). In base c you work with char pointers which point to a section of allocated memory. A string array is terminated, by convention, with a null character. Thus the c string "abc" is stored in four bytes of which the last is zero. You would declare this as, for example,  char sampleStr[4]="abc";
To copy that string to another you might try:
  char newString[20];
  strcpy(newString, sampleStr);
Then you could tack on another string as
  strcat(newString, "this is more");
which would search for the null in newString and start copying the bytes. But it is the programmers job to make sure that the array is not overwritten which is a VERY bad thing. This is not easy stuff, but is part of the heart of the c language which allows you to get a pointer to something and do with it what you would, like cast it to something else.

I recomemd that you try c++ and use one of the string classes provided which can do much more by dynamically allocating space and warning of overruns.

Good Luck.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-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.

916 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