Field Pointers, Headers and Structs

Discussion in 'Mac Programming' started by larswik, Jan 12, 2011.

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    Wrapping my head around this subject. I am adding to it and then including the malloc that I am learning as I build this test app. This works if I use a DOT operator 'myNewNum->number = 5;' but not the -> I thought I could pick the '.' or '->' but I am getting an "invalid type argument"?

    header file
    Code:
    struct theNumber {
    	int number;
    };
    
    main.c
    Code:
    #include <stdio.h>
    #include"header_test.h"
    
    int main (int argc, const char * argv[]) {
    	struct theNumber myNewNum;
    	
    	myNewNum->number = 5;
    	printf("My number is: %d",myNewNum->number);
    	
    	return 0;
    }
     
  2. lee1210, Jan 12, 2011
    Last edited: Jan 12, 2011

    lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    Which operator you use depends on the left-hand operand. If it is a pointer to a struct you use ->, if it is a struct you use the . operator.

    -Lee

    Edit: I'll try to whip up examples. Not compiled and tested...

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv) {
      struct theNumber {
        int num;
      };
      struct theNumber localNumber;
      struct theNumber *pointerToNumber;
      pointerToNumber = &localNumber;
    
      localNumber.num = 8;
      pointerToNumber->num += 4;
      printf("In local: %d, via pointer: %d\n",localNumber.num,pointerToNumber->num);
      return 0;
    }
    
     
  3. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #3
    I see where I went wrong Lee. Thanks for that simplified example. My goal for the night is to try to incorporate Malloc and recap on the pointer / structs and functions. I got off to a bad start remembering the '.' and '->'

    Thanks!

    -Lars
     
  4. dmi macrumors regular

    Joined:
    Dec 21, 2010
    #4
    structpointer->field
    is equivalent to
    (*structpointer).field
     
  5. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #5
    This is driving me crazy Lee. Your file compiled so I wrote my own following your example and I am getting an error.

    Code:
    #include <stdio.h>
    
    
    int main (int argc, const char * argv[]) {
    	struct theNumber {
    		int number;
    	};
    	
    	struct theNumber myNumber;
    	struct theNumber *prtToNum;	
    	prtToNum = &myNumber;
    	
    	myNumber.number = 5;
    	ptrToNum->number = 8;
    	
    	printf("My number is: %d", myNumber.number);
    	printf("My pointer number is: %d", ptrToNum->number);
    	return 0;
    }
     
  6. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #6
    You've not spelled ptrToNum consistently. In the declaration and the line following you've spelled it prtToNum (I read this as printer to number:D).

    BTW Are you coding in XCode? If you are, use the LLVM compiler instead of GCC. It gives much more informative error messages. For example, GCC gave the error "ptrToNum undefined" while LLVM gave the error "Use of undeclared identifier 'ptrToNum'; did you mean 'prtToNum'?".
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    When you get an error, always post the text of the error.

    That said, fix your spelling (hilited in red). prt is not ptr (big font for emphasis). When you can't see what's causing an error like this, I always recommend switching to a much larger font in editor. Even done temporarily, it can make you notice things you didn't before.
     
  8. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #8
    ahhhhhh! That would be my dyslexia kicking in. I was going nuts :)

    The book is great but it never covers the Xcode beyond running the program. That sounds like like I should change it, is that option located in the Prefs?

    -Lars
     
  9. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #9
    The compiler is a project build setting.

    To change it:
    1. On the left-hand side of the project window, under Groups & Files, select the blue project icon at the top of the tree.
    2. Click the Get Info button in the toolbar. It's the blue circle with white i. The Project Info window should pop up.
    3. Click the Build tab, if necessary.
    4. Under Compiler Version, change C/C++ Compiler Version to LLVM compiler.

    Optionally, while you're there, you might want to enable all compiler warnings.
    1. Scroll the build settings down to the bottom.
    2. Under LLVM compiler warnings, click+wait+click the empty area next to Other Warning Flags.
    3. Enter -Wall (that's hyphen, capital W, then the word "all", no spaces).

    After you do either of these, you might want to clean (shift+cmd+K) and then build again.
     
  10. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #10
    Got it changed. It's odd to be closing in on the end of the book and know so little about Xcode and compiling beyond pushing the build and go button.

    -Lars
     
  11. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #11
    I wouldn't try to influence you to change course now, but I always think learning to compile from the commandline, regardless of language. I think learning an IDE while learning a language WHILE learning to program can sometimes distract from the task at hand.

    -Lee
     
  12. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #12
    First time using malloc tonight. Can someone check this to make sure I did it right. I am not sure of the last line of code, where the memory block is located. I am unsure if you use the %p for this or not.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define kComment 100
    #define kTitle 30
    
    struct myDVDs {
    	char title[kTitle];
    	char comment[kComment];	
    };
    
    
    int main (int argc, const char * argv[]) {
    	int howBig;
    		
    	struct myDVDs *space;
    	
    	space = malloc(sizeof(struct myDVDs));
    	
    	howBig = (sizeof(struct myDVDs));
    	printf("The size of the memory block is %d\n", howBig);
    	
    	printf("Type under ten letters:");
    	fgets(space->title,kTitle,stdin);
    	printf("You wrote: %s", space->title);
    	
    	printf("The memory block is located %p", space);
    	return 0;
    }
    
    -Lars
     
  13. lee1210, Jan 13, 2011
    Last edited: Jan 13, 2011

    lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #13
    Why ask the user to enter ten characters when they could enter kTitle-1? You never free the memory you malloc. This is small and exits immediately, but every malloc needs a free. %p is the proper format specified for printing a pointer. This will display the address in hexadecimal.

    -Lee

    Edit: My original reply was in the middle of the night. If you're going to have a structure that stores a single DVD's information it might be better to call it myDVD, etc. Having it be plural seems to indicate that it's storing more than one. And it gets awkward to say "this is a list of myDVDs-es".
     
  14. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #14
    Thanks Lee, This was just a test to see if I understood it correctly. Maybe I should be more through when writing the printf to match char arrays and such. You are right I did not free up malloc.

    For the first time in the book the code written in the book did not match the code in his examples.

    Book wrote this.
    infoPtr = malloc( sizeof( struct DVDInfo ) );

    Sample file was this.
    infoPtr = (struct DVDInfo *)malloc( sizeof( struct DVDInfo ) );

    To make sure my terminology is right in the 'book' version, infoPtr returns the block of memory the size of struct DVDinfo, right?

    The sample file leaves me scratching my head. malloc is creating a block of memory the size of struct DVDinfo but I loose it at the pointer?

    -Lars
     
  15. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #15
    This is just to silence a potential compiler warning.

    malloc returns a void *. This is not a pointer to nothing, but a pointer to potentially anything. In other words, the type of thing being pointed to is unknown/unrestricted.

    The (struct DVDInfo *) in the sample file is a type cast. A type cast changes one type into another type. Here it's changing the void * into a struct DVDInfo *.

    Without the cast, the compiler may warn you that you're assigning a void * (the result of malloc) to a struct DVDInfo * (the type of infoPtr).

    With the cast, you've changed to the pointer to a struct DVDInfo * which matches the type of infoPtr, so the compiler doesn't emit a warning.
     

Share This Page