Strings!

E

Emmanuel Delahaye

Il Prof wrote on 04/08/04 :
I'm new in C language (but not in structured programming) and i have
some questions about strings:

1) What of this two string access methods is faster?
- Direct access of the elemtns of an array of char (myarray)
- Access through a pointer to char (*mypointer)


It depends on the implementation.
2) Is more (time) efficient the allocation of strings in the heap or in
the stack?

It depends on the implementation.
3) What is the correct method of assign a new value to a string? Perhaps
the using the strcpy of the new value in the ols string?

It depends on the usage.

Pointing to a different string is fast and efficient, but it's not
always possible.

char * p = "hello";

if (language == FRENCH)
{
p = "boujour";
}

4) Exists a function that return a substring (from a start element to a
finish element) of a string?

Not directly, but a smart usage of strncat() can help a lot.
5) There are some other (non ANSI standard) C libraries that are known
as very useful and often used in practical programming? Some web links
on this?

Probably. http://www.snippets.org/ comes to my mind.
I have some here too:

http://mapage.noos.fr/emdel/clib.htm
Module STR (drop me an email if you are stuck)

Google is your friend.
 
F

Foobarius Frobinium

Il Prof said:
Hi!

I'm new in C language (but not in structured programming) and i have
some questions about strings:

1) What of this two string access methods is faster?
- Direct access of the elemtns of an array of char (myarray)
- Access through a pointer to char (*mypointer)


Probably the latter. Because with the former method, the array will
decay into a pointer to its first element, i will be added, and then
derefenced anyway.
2) Is more (time) efficient the allocation of strings in the heap or in
the stack?

Don't know.
3) What is the correct method of assign a new value to a string? Perhaps
the using the strcpy of the new value in the ols string?

Yes. Strings can not just be assigned in any manner like in other,
higher-level programming languages. One could use strcpy. One could
also use malloc and pointer a pointer to char at what it allocates.
e.g.

unsigned char *foo = (unsigned char *)malloc(80 * sizeof(char));

And then assign chars to the elements of that segment of data through
foo.
4) Exists a function that return a substring (from a start element to a
finish element) of a string?
strstr?


5) There are some other (non ANSI standard) C libraries that are known
as very useful and often used in practical programming? Some web links
on this?

GLib, which is part of the gtk project at www.gtk.org
If you want to extend your C programs with scripting language, see
www.lua.org.
 
E

Emmanuel Delahaye

Foobarius Frobinium wrote on 08/08/04 :
unsigned char *foo = (unsigned char *)malloc(80 * sizeof(char));

Why do you write so complicated code ? What's wrong with

char *foo = malloc(80);

or simply

char foo[80];
 
P

Paul Hsieh

Il Prof said:
I'm new in C language (but not in structured programming) and i have
some questions about strings:

1) What of this two string access methods is faster?
- Direct access of the elemtns of an array of char (myarray)
- Access through a pointer to char (*mypointer)


Modern compilers will literally equate the two. I.e., there should be
no difference.
2) Is more (time) efficient the allocation of strings in the heap or in
the stack?

malloc() requires dynamic acquisition of memory which requires some
algorithm to run. If by "stack" you mean auto declarations, then such
memory is usually acquired implicitely, and usually requires no cost
at all.

But you have to understand that semantically the two are different and
you aren't always going to have to option of using one to replace the
other.
3) What is the correct method of assign a new value to a string? Perhaps
the using the strcpy of the new value in the ols string?

strcpy() performs an overwrite of a char buffer with the contents of
another char buffer (they buffers cannot legally overlap.) So if you
use it, you need to acquire a sufficiently sized buffer for the target
string memory first (from an auto buffer, a static/global buffer, or
malloc'ed memory.) Another way of "assigning" a new string value is
to simply copy pointers, however, this generates a reference based
copy (meaning that changes to one will obviously lead to changes in
the other.) So which one is more "correct" depends on the semantics
you want from the operation.
4) Exists a function that return a substring (from a start element to a
finish element) of a string?

In general, not really. What you can do, is acquire seperate memory
for a destination substring, then do something like memcpy(dst, src +
start, len); to copy the substring inner contents, then complete the
operation with dst[len] = '\0';
5) There are some other (non ANSI standard) C libraries that are known
as very useful and often used in practical programming? Some web links
on this?

I have written one:

http://bstring.sf.net/

Certain things such as assignment (bAssign), copying (bstrcpy),
substrings (bmidstr/blk2tbstr) are somewhat more obvious than the way
the C Library works.

However there are many others worthy of consideration:

http://www.and.org/vstr/
http://cr.yp.to/lib/stralloc.html
http://www.annexia.org/freeware/c2lib/index.msp
http://firestuff.org/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,372
Latest member
LucretiaFo

Latest Threads

Top