TIP: Click on subject to list as thread! ANSI
echo: locsysop
to: Bob Lawrence
from: Roy McNeill
date: 1994-06-08 19:51:08
subject: C

BL> Hi Roy,

 BL>         I'm still not on top of pointers.



Don't despair. It takes time.



 BL> every time I try to use them

 BL> I manage to lock the computer. Why can't I define:

 BL>

 BL> char source[16]

 BL> strcpy(source,"3:711/934.12");



fine, but the shortcut

	char source[] = "3:711/934.12";

is quicker



 BL> and then use

 BL>

 BL>   strncpy(dest,source[2],3);



Here, you're running foul of C's idea of strings, chars, and

pointers.



source and dest are both pointers to the starts of their respective

character strings in mem. The "source" *string* may be

"3:711/934.12", but the variable "source" is a pointer, and

contains the *memory address* of the first char of the string.



"source[0]" is different. It isn't a pointer. It's picked up the mem

address of the start of the string from the variable "source",

added the index (zero), and plucked the char from mem. "source[0]"

is a char variable, where "source" is a pointer to char. Clear as

mud?



Back to your example: The variable "source[2]" is a character. It

is a char type (not a char* type, like "source" is), and has the

value '7". The compiler has cracked up because you've asked for



 strncpy(char*,char,size_t)  /* size_t is an integer here, ignore */



instead of the legal



 strncpy(char*,char*,size_t)



Hammer this one in: "source" is a pointer. "source[0]"
is a char.



 BL>   I've learned enough about C++ to let my mind go blank as soon as I

 BL> see any part of it, so there was no harm done  ;-). I like printf

 BL> because it was the first thing I got to work, but I'm falling in love

 BL> with puts and her little sister putc.



Whatever works, stick with it for now.



Cheers



--- PPoint 1.80


* Origin: Silicon Heaven (3:711/934.16)
SEEN-BY: 711/934
@PATH: 711/934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.