Correct way of accessing pointer variables of structure

This is a discussion on Correct way of accessing pointer variables of structure within the C Programming forums, part of the General Programming Boards category; Hi , Can any body explain me the wrong thing happening in below code. It is giving me exception. Code: ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    104

    Correct way of accessing pointer variables of structure

    Hi ,

    Can any body explain me the wrong thing happening in below code.

    It is giving me exception.

    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    struct s
    {
    	int a;
     char *s;
    };
    
    int main()
    {
    	struct s *st;
    
    	st->s = (char *) malloc(sizeof(10));
    
    	st->s = "bargi";
    	printf("%s",st->s);
    	getchar();
    
    
    	
    }
    Please explain the wrong thing and the correct way to do it....??

    Thanks
    Bargi

  2. #2
    Registered User
    Join Date
    Jan 2007
    Posts
    104
    Hi,

    It is giving me memory access violation.....but I am allocating it memory ???

    Thanks
    bargi

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You do not have a struct to play with. st is a pointer to a struct s, which means that if you happened to have a struct s hanging around, st could point to it. Since you don't have a struct s, st can't point to anything (and doesn't).

    You could either not make st a pointer (and just declare a struct s normally) or declare a struct s separately and make st point to it.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    104
    hi,

    I have used this:

    st = (struct *) malloc(sizeof(struct s));
    Now it's running fine....
    My question is that....If I have to use pointer variable to access structure members than every time I have to allocate m/m dynamically ??
    Or there is one time solution for it

    Thanks

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    No, because it's a pointer. All it needs is a valid address (which may or may not come from malloc).
    Code:
    int x;
    int* p = &x;
    *x = 5;
    Just as this work, so will it with structs. You should properly learn pointers before using them.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Bargi View Post
    hi,

    I have used this:



    Now it's running fine....
    My question is that....If I have to use pointer variable to access structure members than every time I have to allocate m/m dynamically ??
    Or there is one time solution for it

    Thanks
    Of course you don't have to dynamically allocate memory; you can make the pointer point to a pre-existing structure.
    Code:
    struct s foo;
    struct s *fooptr;
    fooptr = &foo;
    Of course, why you would want to do so does not appear (but the same could be said for your other method too).

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The non-dynamic, local stack version:
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    #include<string.h>
    
    struct s
    {
    	int a;
     char *s;
    };
    
    int main()
    {
    	struct s st;
    
    	st.s = malloc(10);
    
    /*	st->s = "bargi";     <-- this is not C  */
    	strcpy(st.s,"bargi");	
    	printf("%s",st.s);
    
    	return 0;	
    }
    Last edited by MK27; 02-16-2009 at 11:20 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    3
    There are actually three errors and one potential pitfall in this code.

    First, you are creating an uninitialized pointer to a structure. I think you meant do just say:
    Code:
    	s st;

    Second, you are creating a buffer of 4 bytes, the size of an int, instead of "10".
    Code:
    	st->s = (char *) malloc(sizeof(10));
    If you wanted to create an array of 10 bytes, you would just say
    Code:
    	st->s = (char *) malloc(10);
    or
    Code:
    	st->s = (char *) malloc(10 * sizeof(char));
    That is still risky though as it is not tied to the length of the string (if you later changed the string length, you would also have to update the buffer). So better than that, you could write
    Code:
    	st->s = (char *) malloc(strlen("bargi") * sizeof(char) + 1 /* terminator */ );
    Still better though, you could use the strdup function, which does all that for you:
    Code:
    	st->s = (char *) strdup("bargi");
    The third bug is that after creating memory for a string, you leaked that buffer when you assigned st->s to a new const string "bargi".

    You don't state the actual crash, but it was probably due to dereferencing the uninitialized st pointer

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    strdup is non-standard, so it should be avoided.
    And an easier version of
    st->s = (char *) malloc(strlen("bargi") * sizeof(char) + 1 /* terminator */ );
    is just
    st->s = malloc( sizeof("bargi") );
    sizeof(char) is always 1, so might as well omit that one. And sizeof on a string literal works AND it includes the terminating null char, as well. Happy day!
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help...accessing character arrays in a structure
    By mathewmc in forum C Programming
    Replies: 7
    Last Post: 10-31-2006, 10:20 AM
  2. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  3. Funtion pointer in structure
    By Xzyx987X in forum C Programming
    Replies: 1
    Last Post: 07-03-2004, 03:05 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Pointer to a structure
    By frenchfry164 in forum C Programming
    Replies: 5
    Last Post: 03-16-2002, 05:35 PM

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