Solved

strinfs

Posted on 1997-02-19
1
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: 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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

752 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