简体   繁体   中英

Changing a value of a pointer that has been passed to a function

gcc 4.6.2 c89

I am passing a pointer to a function. This pointer will be passed to another function. And depending on the current value, the value will be changed. So in main the value changed will be value changed in the function.

Because I am going through 2 functions. I just wanted to make sure I haven't done anything that would cause undefined behaviour. As this will run on a system with high transcations.

I cannot return the value in the return type, as I need to return TRUE or FALSE so see if the function succeeded or failed.

I have run under valgrind and it reports no errors:

valgrind --leak-check=full --verbose ./ptr

Many thanks for any suggestions,

#include <stdio.h>

#define FALSE 0
#define TRUE (!FALSE)

static int incoming_sdp(int *pcmu_priority);
static int parse_incoming_sdp(int *pcmu_priority);

int main(void)
{
    int pcmu_priority = 10;

    printf("Start program pcmu_priority [ %d ]\n", pcmu_priority);

    if(incoming_sdp(&pcmu_priority) == FALSE) {
        /* Something bad happened */
    }

    printf("Final pcmu priority [ %d ]\n", pcmu_priority);

    return 0;
}

/* Removed the processing that will determine of the function is success of failure */
static int incoming_sdp(int *pcmu_priority)
{
    /* Pass the value of the pcmu_prority */
    if(parse_incoming_sdp(pcmu_priority) == FALSE) {
        return FALSE;
    }

    /* Return true or false if processing was successfull */
    return TRUE;
}

/* Removed the processing that will determine of the function is success of failure */
static int parse_incoming_sdp(int *pcmu_priority)
{
    /* Now change the value of the pcum_priority */
    if(*pcmu_priority == 10) {
        *pcmu_priority = 20;
        printf("pcmu is 10 changing pcmu_priority to [ %d ]\n", *pcmu_priority);
    }
    else if(*pcmu_priority == 20) {
        *pcmu_priority = 10;
        printf("pcmu is 20 changing pcmu_priority to [ %d ]\n", *pcmu_priority);
    }

    /* Return true or false if processing was successfull */
    return TRUE;
}

该程序很好,并且不会产生依赖于未定义行为的输出。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM