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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

a simple pointer question

Status
Not open for further replies.

raj

MIS
Jun 19, 1999
1
US
can anyone please tell me the reason why this message


enter the word abcd
OUTPUT = Borland Intl.

when this code is executed

main()
{
char *abc;
abc = (char *) malloc (sizeof(char) * 30);
printf("enter the word");
scanf( "%[^\n]", abc);
printf("%s",*abc);
}

 
Hi,

seen tha abc is a pointer the character,

the meaning of *abc is a sigle char:

the first in the string pointed by abc.

correct syntax are

printf("%c",*abc);

or

printf("%s",abc);

Is it right ?
 
Along with what victorv mentioned, I always memset my malloc'ed pointers to zero as well, just for safety

memset(abc,0,30);

Just a side note :)

Matt
 
Zyrenthian,

I was under the impression that calloc did that for you. Is there a reason to do the malloc/memset vs calloc? Or is is it just a preference/habit?
 
Just a preference/habbit :) I like to be 100% sure with strings. Cant say how many times it has bit me in the butt because I read off the end of a string.

Matt
 
zyrenthian
could you please explain memset()
what it does?
and also about calloc


thanx
 
memset:

MEMSET(3) Linux Programmer's Manual MEMSET(3)

NAME
memset - fill memory with a constant byte

SYNOPSIS
#include <string.h>

void *memset(void *s, int c, size_t n);

DESCRIPTION
The memset() function fills the first n bytes of the memory area pointed to by s
with the constant byte c.

RETURN VALUE
The memset() function returns a pointer to the memory area s.

CONFORMING TO
SVID 3, BSD 4.3, ISO 9899

SEE ALSO
bzero(3), swab(3)

GNU April 11, 1993 1
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top