Solved

strinfs

Posted on 1997-02-19
1
205 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 50 total points
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now