Re: simple c question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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...

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux