Printing pointer addresses in C [two questions]

前端 未结 5 755
感动是毒
感动是毒 2021-01-04 14:02

I know that my questions are very simple but googleing them didn\'t get me any useful results... They\'r probably too simple!!

No. 1

char* createStr(         


        
相关标签:
5条回答
  • 2021-01-04 14:17

    Comments inline!

    No. 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);
    }
    

    No 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.
    $
    

    Point to note:

    • w.r.t #2 : in main() it should be char * argv[] and not int * argv[]
    0 讨论(0)
  • 2021-01-04 14:28

    To fix the first one:

    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);
    }
    

    Otherwise, you're printing out the address of the variable (str1 and str2), not the address the variable is pointing to.

    I've also added a call to free() to fix the memory leak.

    As to the second one, you need to use atoi() to convert the string to an int:

    printf("and integer arg is : %d.\n", atoi(argv[1]));
    
    0 讨论(0)
  • 2021-01-04 14:28

    For the second exemple : *argv[1] is a char * (i.e "3" )
    If you want to display "3" you have to use printf("%s\n", *argv[1]); or get an integer from the string using atoi().
    Maybe you can even try to write your own version of atoi()!

    0 讨论(0)
  • 2021-01-04 14:31

    The first example allocates the string, then returns a pointer to it. When you do the assignment from the method's return value, you just assign the pointer. You only ever made one string.

    In the second example, you want atoi.

    0 讨论(0)
  • 2021-01-04 14:33

    Question 1:

    The addresses are different because you are taking the addresses of the pointers, not the addresses of the variables held by the pointers. Since str2 exists on the stack of main and str1 on the stack of createStr, their addresses are different.

    You can free str1 by freeing str2, because they point the same location. The address that str1 points to is copied inside str2 when str1 is returned from createStr.

    Question 2:

    Use int value = atoi(argv[1]); This converts a char* to an int.

    0 讨论(0)
提交回复
热议问题