LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-22-2005, 03:08 PM   #1
zero79
Member
 
Registered: Nov 2003
Location: Ohio
Distribution: Debian Unstable
Posts: 460

Rep: Reputation: 30
guidlines on working with strings in C?


Hi all,

I'm looking for information about properly handling and manipulating strings in C. For example, when should you declare a sring with "char str[300]" vs. "char *str".

If you use the character pointer, is it possible that other strings, reals, etc could take over some of the memory occupied by that string thus leading to seg faults and sig 11s? I guess that it's smart to use the character array in that sense, but then again more memory is used than need be.

In that sense, what would a sensible character limit be for holding a string that consists of a file name and path? I was thinking something like 300 should be reasonable. Should the code just error if the path is too long, or is there a better way to gracefully handle this?

Thanks for any thoughts.
 
Old 02-22-2005, 03:54 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
The difference between char *str; and char str[300]; is that the first one can only point to a string, and the second one can actually store a string.

When you do something like char *str = "this is a string";, the string will be stored in a read-only memory area and then the str pointer points to it. That means that you should not (and usually cannot, you'll get a segmentation fault) modify that string.

When you do char str[300] = "this is a string"; then the compiler creates an array of 300 chars and then copies the string into that array. That gives you full control over the contents so the string can be modified or what-not.

Of couse, you can also do something like:
Code:
char str[300] = "this is a string";
char *strptr = str;
That way, instead of the pointer pointing to a string in read-only memory, it's pointing to an array that you can modify.

As far as a reasonable limit for a filename and path, the most graceful way to handle it would be to use the PATH_MAX macro defined in limits.h:
Code:
#include <limits.h>

int main(void)
{
  char str[PATH_MAX];

  return 0;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
splitting strings marri Programming 5 04-08-2005 05:33 PM
strings in c djgerbavore Programming 8 01-11-2005 04:27 PM
Need some guidlines on securing a system BajaNick Linux - Security 5 10-15-2004 02:48 PM
Perl>>working with strings format_c Programming 2 11-15-2001 04:21 PM


All times are GMT -5. The time now is 10:57 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration