Structures, arrays, pointers, malloc.

This is a discussion on Structures, arrays, pointers, malloc. within the C Programming forums, part of the General Programming Boards category; I've been working with pointers a bit, and I've made the following: Code: #include <stdio.h> struct structurevariable{ char a[20]; }; ...

  1. #1
    Novice.
    Join Date
    Oct 2005
    Posts
    88

    Structures, arrays, pointers, malloc.

    I've been working with pointers a bit, and I've made the following:
    Code:
    #include <stdio.h>
    struct structurevariable{
           char a[20];
           };
    int main()
    {
      int count;
      struct structurevariable * var[2]; //An array of 2 strucure pointers is created.
      for (count = 0; count < 2; count++)
          {
          /* The size of var[count] is determined. First a typecast is done to let
          malloc return a pointer-variable of the type "structurevariable". Then malloc
          allocates room for one structure with the size of one "structurevariable." */
           var[count]=(struct structurevariable*)malloc(sizeof(struct structurevariable));
           puts("Enter data: ");
           /* Dot operator requires a variable name. Here -> is used for pointers. */
           scanf(" %s", var[count]->a);  //Will store whatever var[count] points to, in a.
           printf("You entered: %s\n", var[count]->a);
           free(var);    //Gives the memory taken by: var[count] back to the system.
          }
          getchar();
          getchar();
          return 0;
    }
    I've made comments, so that I can understand later more easily how it works.

    I've got a couple of questions about this:

    1. Shouldn't I include the stdlib.h? I noticed I could get away with not including it.

    2.
    Code:
    var[count]=(struct structurevariable*)malloc(sizeof(struct structurevariable));
    Why is in sizeof(struct structurevariable) the "struct" neccesary? I thought I had created a new data type with the name structurevariable (like int or char).

    3. Am I correct when I say that: var[count] will only contain a memoryadress of the users input? If yes, where will the actual input be stored ( I would think in *var[count])?

    4. Why is the code for storing the variables and printing them to screen the same? I'd assume that I'd have to dereference something to gain a value.

    Thank You,

    OmnificienT

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    1. Yes, you should have stdlib.h. You may have "gotten away with it", if your compiler assumes you're not smart enough to put it in yourself. NOTE: This is why you should not cast the result of malloc in C, as you will get an earlier warning when you forget to include stdlib.h.

    2. You thought incorrectly. You have created a new data type with the name "struct structurevariable". If you want a different name, you can typedef it.

    3. var[count] will contain a memory address, yes. That memory address points to a place where your users input will be placed. (IOW: var[count] is a pointer; *(var[count]) is an actual struct structurevariable data object.)

    4. When printf'ing, %s assumes a pointer to the start of the string. Since you give it a pointer to the start of the string (since the name of the array decays to a pointer when used by itself), everybody is happy.

  3. #3
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    Well, thanks for explaining.

    So using struct will create a new type of variable with the name "struct something".

    Only, what is a data object and what is a typedef?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Only, what is a data object and what is a typedef?
    "Data object" just means an object. A typedef is just a way of giving another name to a data type, e.g., instead of using struct animal as the type, we can typedef it to just animal.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    As I was looking through my code again, to make sure I understood, I noticed something with the malloc statement:
    Code:
    var[count]=(struct structurevariable*)malloc(sizeof(struct structurevariable));
    var[count] actually holds a memory adress. So in fact I am determening the size of a pointer variable.
    To make it a bit easier lets take:
    Code:
    int * a;
    a = (int*)malloc(sizeof(int));
    Can I conclude that C is being smart and malloc allocates memory for *a and not for a?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    malloc itself is "stupid", it just gives you back a lump of memory that is the size you asked for. Whether that is for one int (2 or 4 bytes) or a lot one million ints (100 * 100 * 10 * 4) is something only the programmer will know. The compiler doesn't care as such.

    Similarly int *a makes a space to "store the memory location of something, which will be used as an integer".

    Think of memory locations as numbered lockers (such as you'd find in a bank safety deposit box or a rack of lockers for left luggage at stations, gym changing rooms etc). Each memory location contains a number. A pointer is essentially a locker with the number of another locker.

    Say for example we have:
    Code:
    int a;    // Compiler decides to use location 1000
    int *pa;   // Location 1001
    int b;   // Location 1002
    
    pa = &a;   // Set location 1001 to 1000. 
    *pa = 7;   // Read location 1001, use the "locker" number in there to store the 7. 
    pa = &b;   // Set location 1001 to 1002.
    What I haven't bothered with here is that "large objects take up more than one locker".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    Well I do think I understand how the heap and pointers work, only my question is more regarding malloc(). Malloc is memory allocation. So:

    Code:
    char * a; 
    a = malloc(10);
    Will allocate 10 bytes and return a character pointer.

    My question now is: will C allocate 10 bytes of memory space for a, in which a memory adress is stored, or will C allocate 10 bytes of memory for *a where an actual value is stored?

    Either I did not understand the previous reply or matsp didn't understand my question.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Will allocate 10 bytes and return a character pointer.
    Actually, it returns a void pointer which is implicitly cast to a char pointer.

    My question now is: will C allocate 10 bytes of memory space for a, in which a memory adress is stored, or will C allocate 10 bytes of memory for *a where an actual value is stored?
    a is a pointer, so it takes up the amount of space a pointer needs, which is known at compile time. The size of what a points to is (sometimes) only known at runtime, so the 10 bytes of memory allocated is for what a points to.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    To put it another way, when you declare
    Code:
    char * a;
    the compiler sets aside room for a pointer (usually four bytes these days) and that space has the name "a". The malloc call sets aside 10 bytes (the amount you ask for) somewhere else. The address of that new memory is stored as the value of the variable a. So a has an address, somewhere on the stack, and the value of a is another address; that address is ten bytes' worth of characters.

  10. #10
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with malloc and pointers to pointers
    By mike_g in forum C Programming
    Replies: 7
    Last Post: 03-29-2008, 06:03 PM
  2. Problems with pointers and malloc()
    By Deirdre in forum C Programming
    Replies: 3
    Last Post: 10-28-2007, 04:20 PM
  3. use of pointers with char arrays
    By txp200 in forum C Programming
    Replies: 6
    Last Post: 05-12-2004, 06:52 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Array pointers to Multi-Dimensional Arrays
    By MethodMan in forum C Programming
    Replies: 3
    Last Post: 03-18-2003, 08:53 PM

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