【问题标题】:Printing pointer addresses in C [two questions]C中打印指针地址【二题】
【发布时间】:2012-01-26 01:52:12
【问题描述】:

我知道我的问题很简单,但谷歌搜索并没有给我任何有用的结果...它们可能太简单了!

没有。 1

char* createStr(){
    char* str1 = malloc(10 * sizeof(char));
    printf("str1 address in memory : %p\n", &str1);
    return str1;
}

int main(void){
    char* str2 = createStr();
    printf("str2 address in memory : %p\n", &str2);
}

结果:

str1 address in memory : 0x7fffed611fc8
str2 address in memory : 0x7fffed611fe8

为什么 createStr() 函数内外的地址不同,如何释放(str1)???

没有。 2

int main(int argc, int *argv[]){
    printf("Basename is %s ", (char*) argv[0]);
    if(argc > 1 ){
        printf("and integer arg is : %d.\n", (int) *argv[1]);
    }
}

如果我编译并运行$ ./test 3,我怎样才能得到int 3?

结果:

Basename is ./test and integer arg is : 1380909107.

【问题讨论】:

  • 每个问题一个问题,请

标签: c pointers int free command-line-arguments


【解决方案1】:

修复第一个问题:

char* createStr(){
    char* str1 = malloc(10 * sizeof(char));
    printf("str1 address in memory : %p\n", str1);
    return str1;
}

int main(void){
    char* str2 = createStr();
    printf("str2 address in memory : %p\n", str2);
    free(str2);
}

否则,您将打印出变量的地址(str1str2),而不是变量指向的地址。

我还添加了对 free() 的调用以修复内存泄漏。

至于第二个,你需要使用atoi()将字符串转换为int

printf("and integer arg is : %d.\n", atoi(argv[1]));

【讨论】:

  • 谢谢 aix!很好的解释。 :)
【解决方案2】:

第一个例子分配字符串,然后返回一个指向它的指针。当您从方法的返回值进行分配时,您只需分配指针。你只编过一根弦。

在第二个示例中,您需要atoi

【讨论】:

    【解决方案3】:

    问题 1

    地址不同,因为您获取的是指针的地址,而不是指针持有的变量的地址。由于str2存在于main的栈上,str1存在于createStr的栈上,所以它们的地址不同。

    您可以通过释放str2 来释放str1,因为它们指向同一个位置。当str1createStr返回时,str1指向的地址被复制到str2内部。

    问题 2

    使用int value = atoi(argv[1]); 这会将char* 转换为int

    【讨论】:

      【解决方案4】:

      对于第二个例子:*argv[1]char *(即 "3"
      如果要显示"3",则必须使用printf("%s\n", *argv[1]); 或使用atoi() 从字符串中获取整数。
      也许您甚至可以尝试编写自己的atoi() 版本!

      【讨论】:

        【解决方案5】:

        内嵌评论!

        没有。 1

        #include <stdio.h>
        #include <stdlib.h>
        
        char* createStr(){
            char* str1 = malloc(10 * sizeof(char));
            /* str1 is a local variable which is allocated in 
               stack and not in heap */
            /* But the dynamic memory allocation is done in 
               heap so malloc returns a portion of memory from
               heap and str1 is made to point to that!
            */
            /*
            aaaa (stack)    bbbb (heap)
            +--------+      +-+-+-------+-+
            |  str1  |----->|0|1| ..... |9|
            +--------+      +-+-+-------+-+
            */
        
            printf("address of str1 in memory : %p\n", &str1);
            /* prints aaaa and not bbbb */
            /* to print the base address of the allocated memory, 
            printf("str1 address in memory : %p\n", str1);
            */
            printf("address of the allocated memory inside func : %p\n", str1);
        
            return str1;
        }
        
        int main(void){
            char* str2 = createStr();
            /* str2 is a local variable to main and so it is
               allocated in the stack 
            */
            /*
            cccc (stack)    bbbb (heap)
            +--------+      +-+-+-------+-+
            |  str2  |----->|0|1| ..... |9|
            +--------+      +-+-+-------+-+
            */
            printf("address of str2 in memory : %p\n", &str2);
            /* the above will print the address of the str2 
               (which is cccc) but not where it is pointing 
               to (bbbb) ..
            */
            /* So to print the base address of where it is 
               pointing to (bbbb), 
            printf("str2 address in memory : %p\n", str2);
            */
            printf("address of the allocated memory inside main : %p\n", str2);
        }
        

        没有 2。

        #include <stdio.h>
        
        int atoi(char a[])
        {
                int i, n=0;
        
                for (i=0 ; a[i] >= '0' && a[i] <= '9' ; i++)
                        n = 10 *n + (a[i]-'0');
        
                return n;
        }
        
        int main(int argc, char *argv[]){
            printf("Basename is %s ", (char*) argv[0]);
            if(argc > 1 ){
                printf("and integer arg is : %d.\n", atoi(argv[1]));
            }
        }
        
        
        $ gcc atoi.c -o atoi
        $ ./atoi 3
        Basename is ./atoi and integer arg is : 3.
        $
        

        注意点:

        • w.r.t #2 : 在 main() 中它应该是 char * argv[] 而不是 int * argv[]

        【讨论】:

        • @Psyclops:如果这回答了问题,请标记它。点击“勾号”,可以这么说。
        猜你喜欢
        • 2020-06-07
        • 1970-01-01
        • 2021-07-07
        • 2010-11-06
        • 1970-01-01
        • 1970-01-01
        • 2021-11-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多