variable pass by reference between function in c

Asked

Viewed 57 times

-1

1) Create a program that has a character array. This vector must have a maximum size TAM = 10. No parameter can be global.

The program must have:

a. the main function, responsible for interacting with the user, informing the results of the operations of the other functions

b. a "insert character" function. It must request the character from the user, and insert it into the vector, in the first empty position, if it still has space. Must return if inserted or not.

c. A "character swap" function. This function requests a new character from the user. The function must search in the vector the first character that is larger than the given character, when find, do the switch. The function must return whether there was the switch or not. When there is the switch, the main function (main) must be able to print which character came out of the vector.

d. A function to remove characters. The deletion must be logical at the end of the array. Simply decrement the top.

e. A function to list characters.

f. a function that prints each of the distinct characters and the number of times it appears in the array

I’m trying to create this program in C gives question above, but I’m having trouble passing the flag back to main, the character returns at point 0 of the array, but the flag does not return as 1.

That’s the code I’ve written so far.

void insere(int array[],int n, int flag);

int main(){
    int array[10], n=0, flag=0;

    insere(array, n, flag);
    printf("%d", flag);
    if(flag==1){
        printf("numero inserido");
    }
    return 0;
}

void insere(int array[], int n, int flag){
    int i;

    printf("insira um numero: ");
    for(i=n; n<10; n++){
        scanf("%d", &array[i]);
    }
    flag =1;
    return flag;
}

1 answer

0


The way you defined your function the parameter flag is being passed by value, that is, a copy of the existing value in main is copied to the stack and any modification made to the function is done exclusively on the stack and not on the existing variable in main. For what you want you need to use the parameter passage by reference.

Test as follows:

void insere(int array[],int n, int *flag);

int main(){
    int array[10], n=0, flag=0;

    insere(array, n, &flag);
    printf("%d", flag);
    if(flag==1){
        printf("numero inserido");
    }
    return 0;
}
void
void insere(int array[], int n, int *flag){
    int i;

    printf("insira um numero: ");
    for(i=n; n<10; n++){
        scanf("%d", &array[i]);
    }
    *flag = 1;
}

If the function is void it makes no sense to return a value.

When passing the parameter by reference the value that is placed on the stack is the address of the variable and in this case when you refer to the content pointed by this address, this is an indirect, and then you are changing the value of the variable defined in the main.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.