Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

error in my code but don't know where!

Status
Not open for further replies.

laura22

Programmer
Feb 12, 2006
1
US
static char *my_kstrdup(const char *buf)
{
char *ptr, *ret;

ret = ptr = kmalloc(strlen(buf));
if(ptr = NULL)
panic("kmalloc returned NULL");

for(; *buf != '\0'; ++ptr, ++buf)
*ptr = *buf;

*ptr = '\0';

return ret;
}

static char *my_toupper(char *c)
{
if(c >= 'a' && c <= 'z')
return c - 'a' + 'A';
return c;
}

void complex_hello(void)
{
const char *msg = "hello World!!!";
char *copy;

copy = *my_kstrdup(msg);

/* Capitalize the first letter 'Hello World!!!'*/
copy[0] = *my_toupper(copy[0]);


kprintf("%c\n", copy);


}
 
Code:
ret = ptr = kmalloc(strlen(buf));
should be:
Code:
ret = ptr = kmalloc(strlen(buf) + 1);

This function might have problems on certain platforms:
Code:
static char *my_toupper(char *c)
 
Not to mention
> if(ptr = NULL)
should be == here.


--
 
> And are you allowed to ++buf when buf is declared as const?
Yes, because in this context the thing which is const is the thing which is being pointed at.

buf++ is ok, the pointer is being incremented.
(*buf)++ is not, because what buff points to is being incremented.

Declaring either
[tt]const char * const buf
char * const buf[/tt]
on the other hand would prevent buf++

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top