Thread: Question: Is a structure and/or a pointer one word?

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

    Question: Is a structure and/or a pointer one word?

    I know that with arrays when intitiated create a word with an addressable address to the array as a whole, and minor address to the elements in the array.
    Like code below
    Code:
    int main(void)
    {
    int i,data[4] = {4, 3, 0, 2};
    
           for(i = 0; i < 4; i++)
          {
    	printf("The whole arrays address is %p , the address of each element is %p\n",data,data[i] );
         }
    }
    /*output:The whole arrays address is 0033FB88 , the address of each element is 00000004
    The whole arrays address is 0033FB88 , the address of each element is 00000003
    The whole arrays address is 0033FB88 , the address of each element is 00000000
    The whole arrays address is 0033FB88 , the address of each element is 00000002*/
    But does the structure on the machine addressable as one word?
    Say in this example below, would struct id john have its own address..
    #include <stdio.h>
    #include <string.h>
    #define MAX 999
    struct id{
    char name[20];
    int age;
    } john;

    And is far is pointer go they don't have an address unless pointed to directly to an data type or object type(address) or dynamically allocated with malloc. So does technically does that mean a pointer is not a word on the machine, when just declared only?

    thanks, for any input, I am just a begineer and this c by discovery book doesn't actually tell you much about what is going on in the ram with words...

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    All variables have addresses, it doesn't matter if it's a pointer or not.
    Code:
    int x, *p;
    
    printf( "address of x is: %p\n", &x );
    printf( "address of p is: %p\n", &p );
    Other than that, I'm not sure what you're trying to ask.


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

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> printf("The whole arrays address is %p , the address of each element is %p\n",data,data[i] );

    The symbol 'data' resolves to a pointer. When you apply the 'index operator' (eg: '[]'), you are adding a certain offset and then dereferencing the pointer, so in other words you get "the data itself". To get the address of that data, you'll need to use the 'address-of operator' (eg: '&'). Maybe this helps:

    data[ i ] == *( data + i ) // the actual element itself
    - so -
    &data[ i ] == ( data + i ) // the address of the element

    >> So does technically does that mean a pointer is not a word on the machine, when just declared only?

    No, any form of information requires some amount of storage. For modern machines the size of a pointer is 4 or 8 bytes, but it varies. At any rate, just think of a pointer as a special kind of integer. The value it holds is an address. If you don't initialize it, it points to nowhere in particular, and that, of course, is a very bad thing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing structure by reference or pointer?
    By 7force in forum C Programming
    Replies: 8
    Last Post: 12-13-2010, 06:49 PM
  2. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  3. pointer to pointer as argument question
    By Lateralus in forum C Programming
    Replies: 4
    Last Post: 07-21-2005, 05:03 PM
  4. Funtion pointer in structure
    By Xzyx987X in forum C Programming
    Replies: 1
    Last Post: 07-03-2004, 03:05 AM
  5. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 05:40 PM