Pointer Problems

This is a discussion on Pointer Problems within the C Programming forums, part of the General Programming Boards category; I'm having issues understanding how to use pointers, or so my compiler is telling me. Anyway, I'm trying to simply ...

  1. #1
    Registered User TieFighter's Avatar
    Join Date
    Feb 2010
    Location
    La Crosse, WI
    Posts
    32

    Unhappy Pointer Problems

    I'm having issues understanding how to use pointers, or so my compiler is telling me. Anyway, I'm trying to simply take whatever Attributes are given to me in the parameters, malloc some space for them so they don't disappear at the end of the function, and then set the Attribute part of a SymEntry to reference that newly allocated space.

    Code:
    void SetAttr(struct SymEntry *AnEntry, void *Attributes) {
    	AnEntry->Attributes = malloc(sizeof(Attributes*));
    	AnEntry->Attributes = *Attributes;
    };
    While I'm at it, how do I retrieve that attribute?
    Code:
    void * GetAttr(struct SymEntry *AnEntry) {
    	void *Att = malloc(sizeof(AnEntry->Attributes));
    	Att = *(AnEntry-Attributes);
    	return Att;
    };
    This is probably really basic stuff to you guys, but I'm really lost here due to lack of any experience with pointers. Thanks in advance.

    oh and here are the errors cc is giving to me:
    SymTab.c: In function ‘SetAttr’:
    SymTab.c:113: error: expected expression before ‘)’ token
    SymTab.c:114: warning: dereferencing ‘void *’ pointer
    SymTab.c:114: error: void value not ignored as it ought to be
    SymTab.c: In function ‘GetAttr’:
    SymTab.c:119: error: ‘Attributes’ undeclared (first use in this function)
    SymTab.c:119: error: (Each undeclared identifier is reported only once
    SymTab.c:119: error: for each function it appears in.)
    Last edited by TieFighter; 02-18-2010 at 07:59 AM. Reason: Listing Errors

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Hmmm. Where to start...
    First off, how would you call SetAttr?
    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.

  3. #3
    Registered User TieFighter's Avatar
    Join Date
    Feb 2010
    Location
    La Crosse, WI
    Posts
    32
    It looks like SetAttr is called in the following manner (this isn't my code - a driver that was pre-made)
    Code:
          AnAttr = (struct Attributes *) malloc(sizeof(struct Attributes));
          AnAttr->Value1 = Val1;
          AnAttr->Value2 = 1;
          SetAttr(AnEntry,AnAttr);
    If it helps - here is how SymEntry defines Attributes:

    Code:
    struct SymEntry { char *Name;
                      void *Attributes;
                      struct SymEntry *Next;
                    };

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    The first error:
    Code:
    void SetAttr(struct SymEntry *AnEntry, void *Attributes) {
    	AnEntry->Attributes = malloc(sizeof(Attributes*)); // This doesn't make sense, you're allocating MORE space for a void pointer? You've got room in AnEntry already
    	AnEntry->Attributes = *Attributes;  // You can't dereference void pointers. Cast to the type you want then dereference.
    }*snip semi-colon*
    The second error:
    Code:
    void * GetAttr(struct SymEntry *AnEntry) {
    	void *Att = malloc(sizeof(AnEntry->Attributes));
    	Att = *(AnEntry->Attributes);
    	return Att;
    }*snip semi-colon*
    Not that the extra semi-colons are errors anyway...

    Having a rough idea what you're trying to do:
    Code:
    void SetAttr(struct SymEntry * AnEntry, void * Attributes, size_t len)
    {
       AnEntry->Attributes = malloc(len);
       memcpy(AnEntry->Attributes, Attributes, len);      /* careful, pointer members of 'Attributes'
                                                             may be free'd later */
    }
    
    void * GetAttr(struct SymEntry * AnEntry)
    {
       return AnEntry->Attributes;
    }
    Or you could just set Attributes to the passed 'Attributes' argument in SetAttr() and be done with it.
    Last edited by zacs7; 02-18-2010 at 04:06 PM.

  5. #5
    Registered User TieFighter's Avatar
    Join Date
    Feb 2010
    Location
    La Crosse, WI
    Posts
    32
    Quote Originally Posted by zacs7 View Post
    Code:
    void SetAttr(struct SymEntry * AnEntry, void * Attributes, size_t len)
    {
       AnEntry->Attributes = malloc(len);
       memcpy(AnEntry->Attributes, Attributes, len);      /* careful, pointer members of 'Attributes'
                                                             may be free'd later */
    }
    Or you could just set Attributes to the passed 'Attributes' argument in SetAttr() and be done with it.
    I think that helped a lot - does memcpy just make a duplicate of whatever your copying in a different location in memory? That is slick compared to what I was trying to do before.

    I do have a question about the code segment you suggested above though, is len the length of something in particular? I'm not allowed to change the function signature like that, unfortunately. I can tell you that the user of the function is supposed to worry about memory allocation and freeing space of attributes on their own based on what the driver file does with it.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by TieFighter View Post
    I think that helped a lot - does memcpy just make a duplicate of whatever your copying in a different location in memory? That is slick compared to what I was trying to do before.

    I do have a question about the code segment you suggested above though, is len the length of something in particular? I'm not allowed to change the function signature like that, unfortunately. I can tell you that the user of the function is supposed to worry about memory allocation and freeing space of attributes on their own based on what the driver file does with it.
    In that case, your life just got a whole lot easier:
    Code:
    void SetAttr(struct SymEntry * AnEntry, void * Attributes)
    {
       AnEntry->Attributes = Attributes;
    }
    
    void * GetAttr(struct SymEntry * AnEntry)
    {
       return AnEntry->Attributes;
    }
    Anyway, 'len' was the length of the Attributes argument. So you could pass whatever type argument, or structure and it would be copied. That puts the onus on the user to know what type it is when GetAttr() returns (and cast appropriately). Example usage could have been:
    Code:
    /* ... */
    
    struct Attributes AnAttr;
    AnAttr->Value1 = 2;
    AnAttr->Value2 = 1;
    
    SetAttr(Entry, &someStructure, sizeof(someStructure));
    
    /* ... */
    struct Attributes * something = NULL;
    something = GetAttr(Entry);
    
    printf("Value is %d\n", AnAttr->Value1);
    Of course, whatever method you choose you still want to do error checking so you don't dereference NULL. That is... *(NULL) or NULL->Value1 is BAD.

  7. #7
    Registered User TieFighter's Avatar
    Join Date
    Feb 2010
    Location
    La Crosse, WI
    Posts
    32
    Thank you very much zacs7. I appreciate your time in helping me through my bungled code. I'm getting a segmentation fault after the driver tries running my code a bit, but I'm sure that's somewhere else in it. I'll be on the hunt for that, but you helped me out tons with this part.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with passing an array of structures by pointer
    By raptor1770 in forum C Programming
    Replies: 9
    Last Post: 11-29-2008, 10:01 AM
  2. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  3. Function and pointer problems
    By Dylancougar in forum C Programming
    Replies: 4
    Last Post: 11-14-2005, 07:59 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. base class pointer problems
    By ... in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2003, 10:27 PM

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