在C中打印指针地址[两个问题]

Sta*_*hta 9 c free int pointers 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);
}
Run Code Online (Sandbox Code Playgroud)

结果:

str1 address in memory : 0x7fffed611fc8
str2 address in memory : 0x7fffed611fe8
Run Code Online (Sandbox Code Playgroud)

为什么地址不同于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]);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

结果:

Basename is ./test and integer arg is : 1380909107.
Run Code Online (Sandbox Code Playgroud)

San*_*raj 9

评论内联!

第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);
}
Run Code Online (Sandbox Code Playgroud)

没有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.
$
Run Code Online (Sandbox Code Playgroud)

注意事项:

  • wrt#2:在main()中应该是char * argv[]而不是int * argv[]


NPE*_*NPE 5

修复第一个:

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);
}
Run Code Online (Sandbox Code Playgroud)

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

我还添加了一个free()修复内存泄漏的调用.

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

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