Like Tree9Likes

Struct and pointer

This is a discussion on Struct and pointer within the C Programming forums, part of the General Programming Boards category; Code: Hello, please someone show me the code for what is asked the in the following program's comments for the ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    13

    Struct and pointer

    Code:
    Hello, please someone show me the code for what is asked the in 
    the following program's comments for the variable ptr.
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <malloc.h>
    
    struct _TEST
    {
        char msg[250];
        int cnt;
    }TEST;
    
    typedef struct _TEST **PTR;
    
    int main(int argc, char *argv[])
    {
        PTR ptr;
        
        // allocate memory
        
        
        // assign values to the struct members
        
        
        // print the values
        
        
        // free the memory
        
        return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    So you're asking for someone to do your homework for you? Sure, I'll bite..

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <malloc.h>
    
    struct _TEST
    {
        char msg[250];
        int cnt;
    }TEST;
    
    typedef struct _TEST **PTR;
    
    int main(int argc, char *argv[])
    {
        PTR ptr;
        
        // allocate memory
        malloc(1);
        
        // assign values to the struct members
        TEST.cnt = 0;
        memset(&TEST.msg, 0, sizeof(TEST.msg));
        
        // print the values
        printf("the values");
        
        // free the memory
        free(malloc(1));
    
        return 0;
    }
    Edit:
    Damn, CommonTater was faster than me

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    13
    Code:
    Well, I am not a student rather an old programmer trying to refresh my C language. I actually have the following but it crashes when I ran it.
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <malloc.h>
    
    struct _TEST
    {
        char msg[250];
        int cnt;
    }TEST;
    
    typedef struct _TEST **PTR;
    
    int main(int argc, char *argv[])
    {
        PTR ptr;
        
        // allocate memory
        if((ptr = (struct _TEST **) malloc(sizeof(struct _TEST))) == NULL) 
            return -1;
            
        // assign values to the struct members
        strcpy((*ptr)->msg, "This is what you think!");
        (*ptr)->cnt = 2011;
        
        // print the values
        printf("%s %d\n", (*ptr)->msg, (*ptr)->cnt);
        
        // free the memory
        free(ptr);
        
        return 0;
    }

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What's the point of doing this:
    Code:
    typedef struct _TEST **PTR;
    
    int main(int argc, char *argv[])
    {
        PTR ptr;
    If you are just doing this:
    Code:
    if((ptr = (struct _TEST **) malloc(sizeof(struct _TEST))) == NULL) 
            return -1;
    You don't even need the cast, and you would have been better just doing:
    Code:
    ptr = malloc( sizeof **ptr );
    Except of course that would still be wrong, because you have a struct foo ** and you are allocating size of struct foo instead of struct foo *.

    FYI: No one likes typedefed pointers.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    You should have posted that code directly even though it doesn't work. Most people tend to look down on "write this for me" type of posts and assume the writer is to lazy to do it him/her self.

    Is there any special reason you have to use a pointer to pointer to _TEST or is that a mistake?
    When you try to dereference it here
    Code:
    strcpy((*ptr)->msg, "This is what you think!");
    You first dereference ptr which points to a struct _TEST, but because you declared it as _TEST** the compiler thinks it points to a pointer to _TEST, which makes the result an invalid pointer.
    You then dereference the invalid pointer with -> which causes the crash.
    Last edited by _Mike; 07-22-2011 at 03:26 PM.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    13
    This is not an assignment nor a project, simply reviewing struct, typedef, and pointers in combination for learning purposes, because in real world you will this kind code a lot!

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    13
    Code:
    It crashes and does not even print "1- debug..."
    
     #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <malloc.h>
    
    struct _TEST
    {
        char msg[250];
        int cnt;
    }TEST;
    
    typedef struct _TEST **PTR;
    
    int main(int argc, char *argv[])
    {
        PTR ptr;
        
        // allocate memory
        if((ptr = malloc(sizeof **ptr)) == NULL) 
            return -1;
        printf("1- debug...\n");
            
        if((*ptr = malloc(sizeof TEST)) == NULL)
            return -2;
            
        printf("2- debug...\n");
        // assign values to the struct members
        strcpy((*ptr)->msg, "This is what you think!");
        (*ptr)->cnt = 2011;
        
        // print the values
        printf("%s %d\n", (*ptr)->msg, (*ptr)->cnt);
        
        // free the memory
        free(ptr);
        
        return 0;
    }

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by rassul View Post
    This is not an assignment nor a project, simply reviewing struct, typedef, and pointers in combination for learning purposes, because in real world you will this kind code a lot!
    So are you saying the real world is full of broken code, or full of typedefed pointers, or both? The real world sounds pretty crappy.


    Quzah.
    AndrewHunter likes this.
    Hope is the first step on the road to disappointment.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by rassul View Post
    Code:
    typedef struct _TEST **PTR;
    
    int main(int argc, char *argv[])
    {
        PTR ptr;
        
        // allocate memory
        if((ptr = malloc(sizeof **ptr)) == NULL) 
            return -1;
        printf("1- debug...\n");
    You are still doing that wrong. If you want a foo** returned, then allocate the size of a foo**, not the size of a foo.
    Code:
    struct foo **p = malloc( sizeof *p ); /* allocate one pointer to foo */
    p[0] = malloc( sizeof **p ); /* allocate one actual structure */
    There's a whole bunch of posts about dynamic 2D arrays around here which cover the types of allocations you are doing. You should probably find some and read them.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by rassul View Post
    It crashes and does not even print "1- debug..."
    printf is buffered and therefore not very good for diagnosing crashes. Print to stderr or better yet use a debugger.

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by quzah View Post
    So are you saying the real world is full of broken code, or full of typedefed pointers, or both? The real world sounds pretty crappy.
    Quzah.
    Quzah, what do you know of the real world? This guy obviously knows what he is talking about, after all he is an 'old programmer trying to refresh on c'. I mean you don't have any real experience. You and Salem are just some geeky teenagers, right?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by AndrewHunter View Post
    You and Salem are just some geeky teenagers, right?
    Yes. I started posting here when I was three.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    2,568
    Quote Originally Posted by quzah View Post
    Yes. I started posting here when I was three.

    Quzah.
    It shows.

    (Here is where the typical person would place a "winked smiley" to show humor. Unfortunately, I'm a curmudgeon who doesn't deal well with that crap. I'll hope the the jest on the sassy response of quzah is understood for what is it - a poor, albeit earnest, humorous response in the language it was received in. And I don't mean 'C'.)

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Matticus View Post
    And I don't mean 'C'.)
    Then how am I supposed to understand it?


    Quzah.
    AndrewHunter likes this.
    Hope is the first step on the road to disappointment.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  2. 2 problems: Struct array and pointer + struct
    By v1n1c1u5 in forum C Programming
    Replies: 0
    Last Post: 12-13-2009, 04:38 PM
  3. Replies: 6
    Last Post: 05-15-2009, 08:38 AM
  4. Replies: 1
    Last Post: 05-05-2004, 06:58 AM
  5. returning a pointer of a struct of a struct array...
    By myrddinb in forum C Programming
    Replies: 1
    Last Post: 04-13-2004, 06:49 PM

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