Regarding accessing the structure member

This is a discussion on Regarding accessing the structure member within the C Programming forums, part of the General Programming Boards category; Hi, please see the below code. Code: struct kk { char a; char name[10]; }; int main() { char* getname(struct ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    13

    Regarding accessing the structure member

    Hi,
    please see the below code.

    Code:
    struct kk
    {
        char a;
        char name[10];
    };
    
    
    int main()
    {
        char* getname(struct kk *,char *);
        struct kk p;
        char *ret = NULL;
        p.a =0;
        strcpy(p.name,"hai");
        ret = getname(&p,"name");
        printf("result : %s",ret);
        return 0;
    }
    
    
    void getname(struct kk *ptr,char *string)
    {
        char *result;
        result=(char *)malloc(10);
        strcpy(result,    );--->what can i put?
        printf("%s",result);
    
    }
    here getname is a function takes structure pointer, member name as inputs .
    purpose of function is to print the value of passed structure member.

    in getname function ,

    strcpy(result, );------ what can i put here as second argument to access the structure member "name".

    is strcpy(result,ptr->string) right one? or strcpy(result,ptr->*string) right one?

    i want to print the string result which is equal to "hai".

    Could you please help me regarding this?

    Thanks in advance,
    Srinu.






    ~

  2. #2
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    108

    Below is the working code

    Code:
    #include <stdio.h>
    
    
    
    struct kk
    {
        char a;
        char name[10];
    };
    
    char* getname(struct kk*,char*);
    
    
    
    
    
    int main()
    {
        struct kk p;
        char *ret = NULL;
        p.a =0;
        strcpy(p.name,"hai");
        ret = getname(&p,"name");
        printf("result : %s",ret);
        return 0;
    }
    
    
    char* getname(struct kk *ptr,char *string)
    {
        char *result;
        result=(char *)malloc(10);
        strcpy(result,ptr->name);
        printf("%s",result);
    		return result;
    
    }

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    13
    Hi vlrk,
    in the getname function,you did not use the string pointer that is second argument.
    i want a output with the help of second argument.

    strcpy(result,ptr->string); or strcpy(result,ptr->*string)..............






    Quote Originally Posted by vlrk View Post
    Code:
    #include <stdio.h>
    
    
    
    struct kk
    {
        char a;
        char name[10];
    };
    
    char* getname(struct kk*,char*);
    
    
    
    
    
    int main()
    {
        struct kk p;
        char *ret = NULL;
        p.a =0;
        strcpy(p.name,"hai");
        ret = getname(&p,"name");
        printf("result : %s",ret);
        return 0;
    }
    
    
    char* getname(struct kk *ptr,char *string)
    {
        char *result;
        result=(char *)malloc(10);
        strcpy(result,ptr->name);
        printf("%s",result);
    		return result;
    
    }

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,765
    Quote Originally Posted by kollurisrinu View Post
    Hi vlrk,
    in the getname function,you did not use the string pointer that is second argument.
    i want a output with the help of second argument.

    strcpy(result,ptr->string); or strcpy(result,ptr->*string)..............
    As vlrk already told you, it's strcpy(result,ptr->string).
    Why?
    Because ptr is a pointer, and therefore we must dereference it first to access its members. So it becomes
    strcpy(result,(*ptr).string)
    It's necessary to use parenthesises due to "*" being lower priority than ".".
    A shortcut for this syntax is "->".

    And let me ask you a question then... why do you need to dereference the member string?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    13
    Quote Originally Posted by Elysia View Post
    As vlrk already told you, it's strcpy(result,ptr->string).
    Why?
    Because ptr is a pointer, and therefore we must dereference it first to access its members. So it becomes
    strcpy(result,(*ptr).string)
    It's necessary to use parenthesises due to "*" being lower priority than ".".
    A shortcut for this syntax is "->".

    And let me ask you a question then... why do you need to dereference the member string?
    Hi Elysia,
    if u use strcpy(result,ptr->string) then u will see error that is

    error: âstruct kkâ has no member named âstringâ.

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by kollurisrinu View Post
    Hi Elysia,
    if u use strcpy(result,ptr->string) then u will see error that is

    error: &#226;struct kk&#226; has no member named &#226;string&#226;.
    Don't be so literal. Elysia meant name, not string. Read your own code!
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 02-12-2009, 01:39 PM
  2. Replies: 2
    Last Post: 04-19-2008, 12:06 AM
  3. Help...accessing character arrays in a structure
    By mathewmc in forum C Programming
    Replies: 7
    Last Post: 10-31-2006, 10:20 AM
  4. problem accessing a struct member
    By ichijoji in forum C Programming
    Replies: 5
    Last Post: 03-09-2006, 09:59 PM
  5. Allocating memory for a structure member
    By dalek in forum C++ Programming
    Replies: 2
    Last Post: 05-15-2003, 06:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21