2012-02-05 35 views
0

gcc 4.6.2 c89更改已传递给函数的指针的值

我传递一个指向函数的指针。这个指针将被传递给另一个函数。根据当前值,值将会改变。因此,主要改变的值将在函数中改变值。

因为我正在经历2个功能。我只是想确保我没有做任何会导致未定义行为的事情。因为这将运行在高转换的系统上。

我不能返回返回类型的值,因为我需要返回TRUE或FALSE,以查看函数是成功还是失败。

我已经在Valgrind的运行,不再报错:

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

的任何建议非常感谢,

#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; 
} 
+0

这可能会更好张贴在[codereview](http://codereview.stackexchange.com) – tinman 2012-02-05 10:38:49

+0

你到底在问什么?如果代码在某些编译器/极端条件下有任何可能导致未定义的行为? 如果是这样,我不能想到通过浏览代码的东西。 只需要在parse_incoming_sdp()函数中记下一点,就没有办法在你当前的方法下实际返回FALSE。这是打算? – Lefteris 2012-02-05 10:49:19

+0

在我发布的代码片段中。我已经删除了所有不相关的代码。所以代码片段是正确的。完整版本将根据值进行一些处理。谢谢。 – ant2009 2012-02-05 10:51:29

回答

1

这个方案是好的,不会产生依赖未定义行为的输出。