#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    Plz help...Really confused... o.O Pointers and malloc() funcn..


    Hi, everybody so here's a program:

    /* Using pointer to access array elements */
    main( )
    {
    char name[ ] = "Klinsman" ;
    char *ptr ;

    ptr = name ; /* store base address of string */

    while ( *ptr != '\0' )
    {
    printf ( "%c", *ptr ) ;
    ptr++ ;
    }
    }

    OUTPUT: Klinsman

    In this program in ptr=name; (BOLD ONE). The output is same even if it's ptr=&name. How is that possible and why? '&' is supposed to be address, right? and 'name' is already equals to the address of name[0],i.e 'name=&name[0]' as "name+0" is equals to "name[0]" and name+0 means just "name" so '&name' would mean '&&name[0]' wouldn't it? Are "&name[0] and &&name[0]" same things? How come the output is same in either cases?
    When I type "&&name[0]", an error is shown: "expected ';' before ']' token. But no error when typed "&name" which is equivalent to "&&name[0]" as "name=&name[0]". :chomp:

    Oh and also there's %c in the printf() function then why does it prints "Klinsman" which is a string??

    P.S.: Can we say & and * gets cancelled like - and +. Like if we say p is a pointer variable and p=&a then *p=*&a and we know *p=a which means * and & got cancelled!! :eh: I know the whole thing behind it but just for the ease of understanding and to make others understand is it safe to say that?






    Oh and please explain this program too:

    /* Program to use array of pointers */
    /* Use of malloc */
    #include <stdlib.h>
    #include <string.h>
    main( )
    {
    char *names[6] ;
    char n[50] ;
    int len, i ;
    char *p ;

    for ( i = 0 ; i <= 5 ; i++ )
    {
    printf ( "\nEnter name " ) ;
    scanf ( "%s", n ) ;
    len = strlen ( n ) ;
    p = malloc ( len + 1 ) ;
    strcpy ( p, n ) ;
    names[i] = p ;
    }

    for ( i = 0 ; i <= 5 ; i++ )
    printf ( "\n%s", names[i] ) ;
    }



    What is malloc() and what is it's purpose in this program? If the "p=malloc(len+1)" line is removed then it prints the name that was input the last(5th) time for five times even though different names were input in the beginning...WHY? Please explain clearly I'm a begineer....
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    Technically &name[0] and name are not equal.
    &name[0] represent address of 1st element of array and name represent address of whole array.
    but actually both address are same.
    but u can see difference when u increment both address.


    ----------------

    malloc is function used to allocate memory dynamically.
    in your example name can be of any size.
    so u cant allocate memory statically using array so that function is used.

    malloc when called will allocate strlen(len+1) byte in hep section and address will be stored in ur pointer.

    Comments on this post

    • Bikal11 agrees
  4. #3
  5. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,367
    Rep Power
    1870
    > In this program in ptr=name; (BOLD ONE). The output is same even if it's ptr=&name.
    The output may be the same, but you should have gotten a compiler warning for your trouble.
    Code:
    $ gcc -std=c99 -Wall foo.c
    foo.c:2:1: warning: return type defaults to ‘int’ [enabled by default]
    foo.c: In function ‘main’:
    foo.c:7:5: warning: assignment from incompatible pointer type [enabled by default]
    eramit2010 is almost right on this.
    ptr = name;
    ptr = &name[0];

    both return the same value and the same type of value (namely a char*, pointing to the first element of the array).

    ptr = &name;
    on the other hand is another thing altogether. Although the value is the same, the type is different - in fact, it's char (*)[9]. You are at this point pointing to the whole array, not just the first element of the array.

    So you would do
    Code:
    char name[ ] = "Klinsman" ;
    char (*ptr)[9] ;
    ptr = &name ; /* store base address of string */
    However, this has knock-on implications for your loop, where now each ptr++ indexes the next array of 9 chars, not the next char.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo