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 7034495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:11:34+00:00 2026-05-28T01:11:34+00:00

I declare typedef struct LABELS_STRUCT { char *name; int address; } `LABELS_STRUCT;` LABELS_STRUCT labels_array[ARRAY_LABELS];

  • 0

I declare

typedef struct LABELS_STRUCT {
char *name;
int address;
} `LABELS_STRUCT;`

LABELS_STRUCT labels_array[ARRAY_LABELS];

and a function that adds a name and address the the aray

int add_data_label(char * label, int displace) 
{
    LABELS_STRUCT x = {label,DATA_STARTING_ADDRESS + (100 * displace)};
    labels_array[initialize]=x;
    initialize++;//=initialize+displace;
    printf("\tAdding [%s] with offset [%d] to labels_array[%d] with address [%d]\n", label,displace,initialize,CODE_STARTING_ADDRESS + (100 * displace));
    printf("\tlabels_array[%d].name=[%s] and labels_array[%d].address= [%d]\n", initialize, labels_array[initialize].name, initialize, labels_array[initialize].address);   

    return 1;
}

The second printf() statement prints out the data correctly but when I have another function

int get_label_address(char *label) {
    int i;
    printf("Getting the Label Address for [%s]\n", label);
    for (i = 0; i < initialize; i++) {
        printf("\t\t\tCOMPARING LABEL [%s] TO [%s] at index [%d]\n", label, labels_array[i].name,i);
        if (labels_array[i].name==label) {
            printf("Label_Array[%d]=[%s] Matches label=[%s] with address [%d]\n",i,labels_array[i].name,label,labels_array[i].address);
            return labels_array[i].address;
        }
    }
    return 0;
}

The debug printf() statement "COMPARING LABEL [%s] to [s]..." Loop shows that its being compared to null. When I use any other function to print the element in labels_array[i].name, it gives me a blank value for the name element but not for the address element. Lets say I have three elements that I put into the array, labey_array[1 2 and 3] will be a blank line but label_array[4 and on] will have null. So it knows it was initialized but its not showing the actual name, just a blank line.

Anyone have any ideas?

  • 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-05-28T01:11:35+00:00Added an answer on May 28, 2026 at 1:11 am

    In function add_data_label() you must do a strcpy() of the incoming char * label to the LABELS_STRUCT‘s char *name;. You should also allocate memory for name in LABELS_STRUCT‘s x using malloc().

    Basically, you need something like the following:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct LABELS_STRUCT {
        char *name;
        int address;
    } LABELS_STRUCT;
    
    #define ARRAY_LABELS 10
    
    LABELS_STRUCT labels_array[ARRAY_LABELS];
    int labels_array_index;
    
    int add_data_label(char * label, int displace) 
    {  
        int len = strlen(label);
        printf("len = %d \n", len);
    
        if ((labels_array[labels_array_index].name = (char *) malloc(sizeof(char) * len + 1)) == NULL) {
            printf("unable to allocate memory \n");
            return -1;
        }
        strcpy(labels_array[labels_array_index].name, label);
        printf("name = %s \n", labels_array[labels_array_index]);
    
        labels_array[labels_array_index].address = displace;
        labels_array_index++;
        /* here you can copy displace to LABELS_STRUCT's address */
    }  
    
    int main(void)
    {  
        labels_array_index = 0;
        add_data_label("abc", 19);
        return 0;
    }
    

    Few points to NOTE:

    • You should free() all the malloc()‘ed memory. So in analogous to add_data_label() you should have delete_data_label() where you call free() for the name.
    • In your original code, I guess initialize is initialized with a valid value
    • In function add_data_label() Statement initialize++; should be written just before return to get debug print statements correct.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a struct like so, typedef struct Player { char *name; char *heroID;
Coding in C: typedef struct { char *string; int one; int two; } Example;
I can declare a structure: typedef struct { int var1; int var2; int var3;
Suppose I have the following struct and function returning a pointer: typedef struct {
Declaring a struct with typedef typedef struct some_struct { int someValue; } *pSomeStruct; and
I'd like to declare a function that returns a pointer to a function of
I need to declare a (typedef'd) structure and a (typedef'd) function reference in pain
typedef struct _stats_pointer_t { char **fileNames; stats_t stats; } stats_pointer_t; I need to fill
If I have the following: typedef struct _MY_STRUCT { int a; float b; }
I have a set of structs, defined as follows: typedef struct { int index;

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.