c - makes pointer from integer without a cast with strcpy -


i can't figure out i'm doing wrong. learning c sorry if wrong, i'm trying use uthash make hash map of stocks , prices. when add stocks hash map, above error.

what did take example site , ran make sure worked, once worked expected changed values fit problem. in original code, variable id in struct integer changed char(instead of number, wanted use stock ticker key), started following errors:

../src/stackcsamples.c:87: warning: passing argument 1 of '__builtin_object_size' makes pointer integer without cast ../src/stackcsamples.c:87: warning: passing argument 1 of '__builtin_object_size' makes pointer integer without cast ../src/stackcsamples.c:87: warning: passing argument 1 of '__builtin___strcpy_chk' makes pointer integer without cast ../src/stackcsamples.c:87: warning: passing argument 1 of '__inline_strcpy_chk' makes pointer integer without cast ../src/stackcsamples.c:89: warning: passing argument 1 of 'strlen' makes pointer integer without cast ../src/stackcsamples.c:89: warning: passing argument 1 of 'strlen' makes pointer integer without cast ../src/stackcsamples.c:89: warning: passing argument 1 of 'strlen' makes pointer integer without cast 

the problem seems 2 lines here(87) strcpy(s->id, user_id); , (89) is: hash_add_str( users, id, s );

how using both of these wrong? looked strcpy , looks takes 3 items, when add size still errors.

here's snippet of parts think relevant:

#include <stdio.h>   /* gets */ #include <stdlib.h>  /* atoi, malloc */ #include <string.h>  /* strcpy */ #include "uthash.h"  struct my_struct {     char id;                    /* key */     float price;     ut_hash_handle hh;         /* makes structure hashable */ };  struct my_struct *users = null;  void new_stock(char *user_id, float price) {     struct my_struct *s;      s = (struct my_struct*)malloc(sizeof(struct my_struct));     strcpy(s->id, user_id);     s->price = price;     hash_add_str( users, id, s );  /* id: name of key field */ }  int main() {     printf("starting..");     new_stock("ibm", 10.2);     new_stock("goog", 2.2);     return 0; } 

you need provide enough space in structure user id array, or dynamically allocate enough space. instance, might do:

enum { max_id_len = 32 }; struct my_struct {     char id[max_id_len];                    /* key */     float price;     ut_hash_handle hh;         /* makes structure hashable */ }; 

then use:

strcpy(s->id, user_id); 

as long you've checked user_id not longer 31 characters plus null before hand. if you've not done check, should. if refuse check , don't mind truncating overlong user id strings, use:

strncpy(s->id, user_id, sizeof(s->id) - 1); s->id[sizeof(s->id)-1] = '\0'; 

this ensures null termination; using strncpy() alone not. beware, if string (s->id) longer, find code wasting time zeroing out tail end of string.


the residual warnings strlen() hidden hash_add_str() macro, come same problem strcpy() warnings did — s->id field not character pointer or character array. amended structure quash warnings too. sure, you'd have show definition of hash_add_str() , macros invokes.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -