public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* simple c question
@ 2004-09-11 20:13 max
  0 siblings, 0 replies; 3+ messages in thread
From: max @ 2004-09-11 20:13 UTC (permalink / raw)
  To: gcc-help


hi!

i'm new to this list and i hope i'm not bothering you with this simple 
stuff:

i'm having problems with strings.

i want to get a path without filename into a string.

and the code is this:

/////////////////////////////////////////////////

// string.h included of course...

char * GetPath (char * string)
{
	char * path;
	int i, pos=-1;
	
	for(i=0;string[i]!=0;i++)
	{
		if(string[i]=='/')
			pos=i;
	}

	if(pos==-1)
		return NULL;
	
	strncpy(path, string, pos+1);
	
return path;
}

////////////////////////////////////////////

i don't get it to work.

can someone please help me?

thank you!

-- 
-----------------------------------------------
----------------maximilian marcoll-------------
-----------composition | performance-----------
http://icem-www.folkwang-hochschule.de/~marcoll
-----------------------------------------------

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: simple c question
  2004-09-11 21:41 Reuti
@ 2004-09-12 10:39 ` max
  0 siblings, 0 replies; 3+ messages in thread
From: max @ 2004-09-12 10:39 UTC (permalink / raw)
  To: gcc-help




thank you all for your help!


max

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: simple c question
@ 2004-09-11 21:41 Reuti
  2004-09-12 10:39 ` max
  0 siblings, 1 reply; 3+ messages in thread
From: Reuti @ 2004-09-11 21:41 UTC (permalink / raw)
  To: m.marcoll; +Cc: gcc-help

Hi there,

>/////////////////////////////////////////////////
>
>// string.h included of course...
>
>char * GetPath (char * string)
>{
>	char * path;
>	int i, pos=-1;
>	
>	for(i=0;string[i]!=0;i++)
>	{
>		if(string[i]=='/')
>			pos=i;
>	}
>
>	if(pos==-1)
>		return NULL;
>	
>	strncpy(path, string, pos+1);
>	
>return path;
>}
>
>////////////////////////////////////////////

you are not allocating any memory for *path (it's just a pointer), where the 
strncpy could copy anythinkg to.

void GetPath (char* string, char* result)
{
    char *path;

    strcpy(result, string);
    path=strrchr(result, '/');
    if (path==NULL)
        result[0]=0;
    else
        *(path+1)=0;
}

Greetings - Reuti

PS: Once upon the time a lived at the Umstraße...

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2004-09-12 10:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-09-11 20:13 simple c question max
2004-09-11 21:41 Reuti
2004-09-12 10:39 ` max

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).