Computing.Net > Forums > Programming > String in C (confusing)

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

String in C (confusing)

Reply to Message Icon

Name: Hn
Date: February 11, 2002 at 15:02:02 Pacific
Comment:

Hello,
Does anybody like to help me in the following problems ? How to declare a variable of string using pointer, instead of array.
eg. char *s++;
this will declare a variable of type pointer to char. But how to declare a pointer to string, so that I can use *str++

In pascal (and delphi i think) I can write:
var str : string;

str = "C String - confusing";
But how to do this in C. The string is not entered from kb, and the following doesn't work:
char str[100];
str ="C String - confusing";

Any help would be thanksful




Sponsored Link
Ads by Google

Response Number 1
Name: Guy
Date: February 11, 2002 at 16:55:20 Pacific
Reply:

C does not have strings, only arrays of characters.

There are a number of library functions to manipulate, compare, etc... these arrays.

They are prototyped in .

For example the following willl work:

char str[100];
strcpy(str,"A dumb string");

If you need more familiar string processing capabilities, then try C++, Java, or almost anything but C.

Regards, Guy


0

Response Number 2
Name:  
Date: February 11, 2002 at 18:14:12 Pacific
Reply:

.
.
char *str;
.
.
str = "I charge thee, Speak!";
.
puts(str);


0

Response Number 3
Name: MS Contin
Date: February 14, 2002 at 12:12:24 Pacific
Reply:


You can also you the C standard library functions malloc and free like

char *d_ptr;
char *s_str = "This is just an arbitrary string";

d_ptr = malloc(strlen(s_str));
strcpy(d_ptr, s_str);
puts(d_ptr);
/* Should write out familiar contents */
free(d_ptr); /* to free memory and lose string reference though d_ptr */

malloc and free are part of the C standard library. The strcpy procedure is in string.h, and should be included the way you would stdlib.h and stdio.h.
If you machine uses other than 1-byte chars (IBM PCs _DO_ use byte-sized chars), you'll have to weight the argument to malloc, or use calloc instead to specify the width of the character you want. Read about these functions in your compiler documentation. If it didn't come with printed docs (many compilers nowadays won't) check any book on C or online help. If you plan on doing any string manipulation with programs written in C, get to know these functions.


0

Response Number 4
Name: MS Contin
Date: February 14, 2002 at 22:50:28 Pacific
Reply:


My fault. Third line in last reply should've been written: d_ptr = malloc(strlen(s_str) + 1);
,ensuring that extra space is granted for the ASCII null byte for end-of-string signal.


0

Response Number 5
Name: Red_Phantom
Date: March 4, 2002 at 17:29:00 Pacific
Reply:

The Response 2 is idiot.It doesnt use malloc().At eesponses 3,4 is missing (char*) before malloc. str=(char *)malloc() because malloc returns void *


0

Related Posts

See More



Response Number 6
Name: XmeX
Date: May 31, 2002 at 19:46:25 Pacific
Reply:

TAN PENDEJOS TODOS.. JAJAJAJA


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: String in C (confusing)

an array of strings in C www.computing.net/answers/programming/an-array-of-strings-in-c/7230.html

String cutting in C www.computing.net/answers/programming/string-cutting-in-c/7277.html

Copying String to String In C www.computing.net/answers/programming/copying-string-to-string-in-c/699.html