I can't figure out what I'm doing wrong. I am learning C so sorry if this is obviously wrong, but I'm trying to use uthash to make a hash map of stocks and their prices. But when I add stocks to my hash map, I get the above error.
What I did was take the example from their site and ran it to make sure it worked, once it worked as expected I changed the values to fit my problem. In the original code, the variable id in the struct was an integer but I changed it to a char(instead of number, I wanted to use the stock ticker as the key), then I started to the following errors:
../src/stackCsamples.c:87: warning: passing argument 1 of '__builtin_object_size' makes pointer from integer without a cast
../src/stackCsamples.c:87: warning: passing argument 1 of '__builtin_object_size' makes pointer from integer without a cast
../src/stackCsamples.c:87: warning: passing argument 1 of '__builtin___strcpy_chk' makes pointer from integer without a cast
../src/stackCsamples.c:87: warning: passing argument 1 of '__inline_strcpy_chk' makes pointer from integer without a cast
../src/stackCsamples.c:89: warning: passing argument 1 of 'strlen' makes pointer from integer without a cast
../src/stackCsamples.c:89: warning: passing argument 1 of 'strlen' makes pointer from integer without a cast
../src/stackCsamples.c:89: warning: passing argument 1 of 'strlen' makes pointer from integer without a cast
The problem seems to be with two lines here(87) which is strcpy(s->id, user_id); and (89) which is: HASH_ADD_STR( users, id, s );
How am I using both of these wrong? I looked strcpy up and it looks like it takes 3 items, but when I add the size I still get errors.
Here's a snippet of the parts I think are 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 this 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;
}
user_id? How is it defined? Also how isHASH_ADD_STRdefined? - Alok SaveHASH_ADD_STRis a macro thats included with uthash. It was originally HASH_ADD_INT but I changed it to str since my main key was not an int. - Lostsoulstrcpytakes in address of the destination and the source strings,the function assumes destination is big enough to hold the string.In your case the destination is acharwhile the source is probably a string, you cannot copy a string to a char, there is not enough memory in a char(1 byte) to hold a string.You could just assign a char to another you don't need copying. - Alok Save