Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8036043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:29:17+00:00 2026-06-05T02:29:17+00:00

I think there’s something basic that I’m not understanding(new to C) regarding strings. Basically

  • 0

I think there’s something basic that I’m not understanding(new to C) regarding strings. Basically I am using uthash and it works when I send strings directly but not from a loop(thats getting its data from an array).

Here’s an example:

enum { MAX_ID_LEN = 5 };
struct my_struct {
    char id[MAX_ID_LEN];                    /* key */
    float price;
    UT_hash_handle hh;         /* makes this structure hashable */
};
struct my_struct *users = NULL;

void new_stock(char *user_id, float price) {
    //printf("%c - %f \n", *user_id, 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.. \n");
    new_stock("IBM", 10.2);
    new_stock("goog", 2.2);
    return 0;
}

That works but when I try to do the same thing from an array it does not(I get no errors when compiling).

char *name_all[] =  {"ibm", "goog"};
int name_all_size =sizeof(name_all)/sizeof(char); 
float price_all[] =  {10.2, 2.2};

enum { MAX_ID_LEN = 5 };
struct my_struct {
    char id[MAX_ID_LEN];                    /* key */
    float price;
    UT_hash_handle hh;         /* makes this structure hashable */
};

struct my_struct *users = NULL;


void insert_data() {
    printf("inserting data \n");
    int data_loc;
    for (data_loc=0;data_loc<name_all_size;data_loc++) {
        //printf("%s - %f \n", name_all[data_loc], price_all[data_loc]);
        //new_stock(name_all[data_loc], price_all[data_loc]);
        //new try
        struct my_struct *s;
        s = (struct my_struct*)malloc(sizeof(struct my_struct));
        strcpy(s->id, name_all[data_loc]);
        s->price = price_all[data_loc];
        //printf("%s - %f \n", s->id, s->price); //ahh displays correctly but still fails
        HASH_ADD_STR( users, id, s );  /* id: name of key field */

    }
}

int main() {
    insert_data();
        return 0;
}

I’m new to C so I am probably assessing this wrong but I think it has to do with the way I’m passing the variables. When I first tried I was sending it to the new_stock function but it was only displaying the first char, so to get around the problem of passing the variable I just moved all the contents of the function into the function I was using to add all the data to but I still get the same problem.

Any idea of what I’m doing wrong?

also just out of personal interest is there any tools that warns me of problems in my code? I find gcc is helpful but once the warnings stop I have no idea how to troubleshoot. Is there something that could help me catch problems like this earlier(something more verbose than gcc). Not sure if its possible but wanted to ask.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T02:29:18+00:00Added an answer on June 5, 2026 at 2:29 am

    Here’s your error:

    char *name_all[] =  {"ibm", "goog"};
    int name_all_size =sizeof(name_all)/sizeof(char);
    

    The array name_all is an array of char pointers, not char, and therefore will only be eight-bytes if you’re on a 32-bit system with 32-bit pointers, and 16-bytes on a 64-bit system with 64-bit pointers. Keep in mind that the string literals being pointed to are not stored in the array, only the pointers to the string literals are in the array. What you really want is:

    int name_all_size = sizeof(name_all)/sizeof(char*);
                                                ^^^^^ note the pointer type
    

    That should give you a value of 2, which is the correct number of elements in name_all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think there must be something basic I'm not understanding about advisory locking in
I'm new to Entity Framework, and I'm think there is something that I misunderstand
I think there is something that I'm just not yet getting about how these
Edit I think there is some confusion, I am not using both of the
I'm having trouble using newly installed gems - I think there must be something
I'm using guidelines right now, but I do not think there is any way
I think there must be something subtle going on here that I don't know
I think there are not many people who don't think that ASP.NET MVC is
I think there is something wrong with my selectors that make my jquery popup
I think there is something I don't understand about same origin limitation for XMLHttpRequest

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.