Solved

strinfs

Posted on 1997-02-19
1
238 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
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

821 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