Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing pointer addresses in C [two questions]

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

Result:

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

Why are the addresses different in and out of the createStr() function and how can I free(str1)???

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

If I compile and run $ ./test 3, how can I get int 3?

Result:

Basename is ./test and integer arg is : 1380909107.
like image 977
Stamoulohta Avatar asked Dec 22 '11 12:12

Stamoulohta


1 Answers

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[]
like image 162
Sangeeth Saravanaraj Avatar answered Oct 29 '22 03:10

Sangeeth Saravanaraj