1

My goal is to copy strings_operand_table1[i] into label. I would then like to return label to my previous function that called it. How can I fix this and what better ways are there to do this?

char GetBaseDeclarationLabel(char *strings_label_table1[], char *strings_mneumonic_table1[],
         char *strings_operand_table1[], int hex_address_table1[])
{
    int i = 0;
    int cmp_str2 = 0;
    char label[20] = {0}; 
    //int k = 0; 
    //printf(" i is %s \n", strings_label_table1[1]); 
    for(i = 0; i < 503; i++)
    {
        if(strings_mneumonic_table1[i] != NULL)`enter code here`
        {
            cmp_str2 = strcmp(strings_mneumonic_table1[i], "BASE");
            if(cmp_str2 == 0)
            {
                //printf(" ??please?? \n");
                //printf(" hex_address_table1[i] is %x \n", hex_address_table1[i]);
                strcpy(label, strings_operand_table1[i]);
                //label = strings_operand_table1[i]; 
                break;
            }
        }

    }
    return label;
}

Edit:

Is this better? If I need to use pointers it seems easier to make the function void and return nothing.

void GetBaseDeclarationLabel(char *strings_label_table1[], char *strings_mneumonic_table1[],
         char *strings_operand_table1[], int hex_address_table1[], char *label1)
{
    int i = 0;
    int cmp_str2 = 0;
    //char label[20] = {0}; 
    //int k = 0; 
    //printf(" i is %s \n", strings_label_table1[1]); 
    for(i = 0; i < 503; i++)
    {
        if(strings_mneumonic_table1[i] != NULL)
        {
            cmp_str2 = strcmp(strings_mneumonic_table1[i], "BASE");
            if(cmp_str2 == 0)
            {
                //printf(" ??please?? \n");
                //printf(" hex_address_table1[i] is %x \n", hex_address_table1[i]);
                //strcpy(label, strings_operand_table1[i]);
                label1 = strings_operand_table1[i]; 
                break;
            }
        }

    }
    //return label;
}

2 Answers 2

1

Change the return type of the function to a char* AND do not return the address of a local.

Either:

  • malloc() the memory for label:

    char* label = NULL;
    
    /*... snip ... */
        if(cmp_str2 == 0)
        {
            label = malloc(strlen(strings_operand_table1[i]) + 1);
            if (label)
            {
                strcpy(label, strings_operand_table1[i]);
            }
            break;
        }
    
  • or avoid memory allocation and simply point label to the matching entry in strings_operand_table1. This is ok as the entries in strings_operand_table1 will exist beyond the scope of the function (and it is simpler for the caller to differentiate between a failed search and failed memory allocation):

    char* label = NULL;
    
    /*... snip ... */
        if(cmp_str2 == 0)
        {
            label = strings_operand_table1[i]);
            break;
        }
    
Sign up to request clarification or add additional context in comments.

2 Comments

Can you please look at my edit when you get a chance?
@cokedude, nope, that won't work. Pass in a char** label and *label = within the function. See c-faq.com/ptrs/passptrinit.html for why.
0

Update function return type from char to char*

char GetBaseDeclarationLabel(char *strings_label_table1[], char *strings_mneumonic_table1[],
         char *strings_operand_table1[], int hex_address_table1[])

to

char* GetBaseDeclarationLabel(char *strings_label_table1[], char *strings_mneumonic_table1[],
         char *strings_operand_table1[], int hex_address_table1[])

Note:

Here you are returning pointer of local array. See when your function returns that local array's memory will be no longer so returning that pointer is useless.

Use heap memory. Malloc for that array and return pointer of that array and after use do not forget to free it.

1 Comment

Can you please look at my edit when you get a chance?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.