c - Print from pointer? -
ok, have assignment gives me constant:
const char *suits[] = {"hearts", "diamonds", "clubs", "spades"};
basically table pointer points 4 words! simple right?
then have import each word table!
so create new table:
char table[30];
in main structure, , want somehow import within word "diamonds"! on pointer's table word on sec place. suits[1].
well when seek print sec word using command:
printf("%s", *suits[1]);
i error. using command
printf("%c", *suits[1]);
i "d", first letter. have ideas on how able print whole word "diamonds", , how can re-create table create in main form?
(i need re-create word suits[1] new table , able print table)
thank much!!!
i'm not clear on mean "copy table", on printing string, can help there.
executive summary:printf("%s", suits[0]); // prints diamonds
what string in c, , memory like? we have code:
const char *suits[] = {"hearts", "diamonds", "clubs", "spades"};
in memory is:
suits[0] -> pointer memory contains {'h', 'e', 'a', 'r', 't', 's', '\0'} suits[1] -> pointer memory contains {'d', 'i', 'a', 'm', 'o', 'n', 'd', 's', '\0'} ...
a string in c refers pointer null terminated piece of memory. when want print string, do:
printf("%s", <a pointer null terminated string>);
in case, pointer found @ suits[0]
, do:
printf("%s", suits[0]);
thoughts on copying table. if want store string "hearts"
in array, perhaps you're looking for:
snprintf(table, sizeof(table), "%s", suits[0]);
or
strncpy(table, sizeof(table), suits[0]);
c pointers
No comments:
Post a Comment