Airon Zagarella
Airon Zagarella

Reputation: 717

Array typedef Struct not functioning properly when called by function

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?

Upvotes: 0

Views: 446

Answers (1)

Sangeeth Saravanaraj
Sangeeth Saravanaraj

Reputation: 16597

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.

Upvotes: 1

Related Questions