Thread: variable addresses and pointers

  1. #1
    1 == 1 tzpb8's Avatar
    Join Date
    Jul 2006
    Posts
    36

    variable addresses and pointers

    Here is the program (follow comments plz ):
    Code:
    //int i and int *(_4zz+1) has the same address??
    //if i remove either int a or int b i get an error and program crashes
    //if i use the int i variable it affects the int *(_4zz+1) pointer
    //anyone know what is wrong? plz help me :)
    //post here or PM ME: [email protected]
    
    #include <stdio.h>
    int g50v3Rz(int *_4zz);
    
    int main(){
    /*int a and int b defined here.. dont know why, but i cant remove them
    and suspect it has with the memory address of "i" to do with... :(*/
    int a,b,i,q = 0,*_4zz = &q;
    for(i=0;i<5;i++)*(_4zz+i) = i;
    for(i=0;i<5;i++)printf("%d\n",*(_4zz+i));
    g50v3Rz(_4zz);
    printf("\nMain\n\n");
    //cant loop using *(_4zz+i) cause if i use i, i get wrong results
    //weird bug or something i did wrong i dont know :(
    printf("%d\n%d\n%d\n%d\n%d\n",*(_4zz),*(_4zz+1),*(_4zz+2),*(_4zz+3),*(_4zz+4));
    //here i confirm that int i and *(_4zz+1) has the same address -or so i think
    printf("\n%x %x %x\n",*(_4zz+4),*(_4zz+1),i);
    printf("\n%d\n%d\n%d\n%d\n%d\n",*(_4zz),*(_4zz+1),*(_4zz+2),*(_4zz+3),*(_4zz+4));
    return 0;}
    
    int g50v3Rz(int *_4zz){
    int x;
    printf("\ng50v3Rz\n\n");
    for(x=0;x<5;x++)printf("%d\n",*(_4zz+x));
    *_4zz = 110;
    //here is int *(_4zz+1) used
    *(_4zz+1) = 221;
    *(_4zz+2) = 332;
    *(_4zz+3) = 443;
    *(_4zz+4) = 554;
    printf("\n");for(x=0;x<5;x++)printf("%d\n",*(_4zz+x));}
    Last edited by tzpb8; 07-26-2006 at 12:47 PM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Wow. I'm speechless. Stay tuned, I'll get my voice back shortly.

    [edit]
    I can only hope that your formatting isn't that awful. I can only hope that your incorrect use of code tags destroyed the readability. I know I'm wrong, mostly because of your brace locations and variable names, but I can still dream, right?

    The single biggest problem with your code is undefined behavior. You're treating a pointer to a single integer as if it were a pointer to an array of five integers. The following fixes that:
    Code:
    #include <stdio.h>
    
    void g50v3Rz ( int *a );
    
    int main ( void )
    {
      int q[5] = {0};
      int *a = q;
      int i;
    
      for ( i = 0; i < 5; i++ )
        *(a + i) = i;
    
      for ( i = 0; i < 5; i++ )
        printf ( "%d\n", *(a + i) );
    
      g50v3Rz ( a );
      printf ( "\nMain\n\n" );
    
      for ( i = 0; i < 5; i++ )
        printf ( "%d\n", *(a + i) );
    
      return 0;
    }
    
    void g50v3Rz ( int *a )
    {
      int i;
    
      printf ( "\ng50v3Rz\n\n" );
    
      for ( i = 0; i < 5; i++ )
        printf ( "%d\n", *(a + i) );
    
      *a = 110;
      *(a + 1) = 221;
      *(a + 2) = 332;
      *(a + 3) = 443;
      *(a + 4) = 554;
    
      printf ( "\n" );
    
      for ( i = 0; i < 5; i++ )
        printf ( "%d\n", *(a + i) );
    }
    Beyond this, I have no idea what question you're asking. Be more specific, and don't do it with comments, please.
    [/edit]
    My best code is written with the delete key.

  3. #3
    1 == 1 tzpb8's Avatar
    Join Date
    Jul 2006
    Posts
    36
    Thanks for the reply, your the greatest
    I know it would work with defining it as a array instaid but i wanted to avoid that to see if i really knew what pointers are
    But i think i figured out.. the "int *(_4zz+1)" variable has the same memory address as "int i" because of something allocating it as this, i'll just have to change the memory address so support +1 +2 +3 +4 additional memory address allocations hmm... wonder why i didn't think of that
    [edit] Now that i think about it, how do i change the memory address of the int variable "i" ?
    Do i have to make it an pointer? :S[/edit]
    Anyways thanks again about the "code" formating i used < > these but i'm supposed to use these "[ ]" instaid? i reread the message when entering the message board test (with "[ ]"):
    Code:
    #include <stdio.h> 
    int main(){printf("test");return 0;}
    test (with "< >"): <code>#include <stdio.h>
    int main(){printf("test");return 0;}</code>

    Seems like it is "[ ]" that is to be used, but then it isn't html tags right? hmm
    Last edited by tzpb8; 07-26-2006 at 09:23 AM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >the "int *(_4zz+1)" variable has the same memory address as "int i" because of something allocating it as this
    Kinda sorta. Since q is only a single integer, any arithmetic on _4zz will either move the pointer implicitly to one of the other variable's addresses (which would trash your own memory), or outside of your address space (which hopefully will crash the program). By making q an array, you guarantee that there's enough memory reserved for your arithmetic on _4zz.

    >Now that i think about it, how do i change the memory address of the int variable "i" ?
    You're missing the point. Arithmetic on a pointer that exceeds the boundaries of the object being pointed to is broken. Since q is a single integer, you can't meaningfully perform arithmetic on _4zz. If q is an array, you can perform arithmetic on _4zz as long as it doesn't exceed the boundaries of the array.

    >Seems like it is "[ ]" that is to be used, but then it isn't html tags right?
    Right, it's BB code.
    My best code is written with the delete key.

  5. #5
    1 == 1 tzpb8's Avatar
    Join Date
    Jul 2006
    Posts
    36
    Thanks, i think/hope that i can see abit more clearly now
    I cant assign a specific memory address to a variable, and i'll have to use arrays if it collides with another used memory address.
    To bad i thought i could replace arrays with pointers sence i read they were the same, but arrays points constant and pointers not?
    Last edited by tzpb8; 07-26-2006 at 10:09 AM.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i thought i could replace arrays with pointers
    You can. By dynamically allocating memory to a pointer, you can simulate an array.

    >sence i read they were the same
    They're most certainly not. People who believe this (and sadly, subsequently teach it to unsuspecting beginners) don't understand that most uses of an array will turn it into a pointer to the first element. However, this is a conversion, not a natural attribute. If you use a pointer as if it were an array, or an array as if it were a pointer, you're sure to get burned when you stumble across one of the cases where they're not equivalent.

    So, if you need a pointer, use a pointer. If you need an array, use an array. Trying to mix and match them without a strong understanding of how they work is inviting failure.
    My best code is written with the delete key.

  7. #7
    1 == 1 tzpb8's Avatar
    Join Date
    Jul 2006
    Posts
    36
    Now i get abit confused, arrays having pointers in memory? pointers can be of any type so this gets wild, guess i'll have to write a test program to see what you are going on at

  8. #8
    1 == 1 tzpb8's Avatar
    Join Date
    Jul 2006
    Posts
    36
    Well here my first such try is (though the pointers are of the same type as the array)
    And it works fine so far.
    Code:
    #include <stdio.h>
    
    int main(void){
    int i,r = 2,s = 4,q = 8,*n = &r,*o = &s,*p = &q,array[3] = {*n,*o,*p};
    for(i=0;i<3;i++)printf("%d\n",array[i]);
    printf("test");
    }

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >arrays having pointers in memory?
    To keep things simple, just remember that a pointer is a variable that holds an address and an array is a collection of variables. As such, you can have an array of pointers, since a pointer is nothing more than another variable.
    My best code is written with the delete key.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Either he is a fast learner or he is playing us.
    One of your alter egos? That would certainly explain the awful coding style.
    My best code is written with the delete key.

  11. #11
    1 == 1 tzpb8's Avatar
    Join Date
    Jul 2006
    Posts
    36
    Another program, this time with different types
    Code:
    #include <stdio.h>
    
    int main(void){
    char s = 'F',*n = &s;
    float r = 3.3,*o = &r;
    int i,q = 7,*p = &q,array[3] = {*n,*o,*p};
    /*if i uncomment this comment it will give an error
    because i cant convert int to float 
    for(i=0;i<3;i++)printf("%d\n",array[i]);  <==== ofcourse*/
    //but this loop is just for lazyness and is quiet stupid i'll have to print
    //them out as different types then it will work.
    printf("%c%.1f%d",*n,*o,*p);
    printf("\n\ntesty");
    //confirmed, seems arrays can hold different types of pointers
    //my (Dev-C++ 4.9.9.2) compiler gives an error:
    /*
    "In function 'int main()':
    [Warning] converting to 'int' from 'float'" -but the program works
    */
    //now to find a flexible/easy way to print out the content
    //of each memory address in the array of pointers with different types.
    }
    Sorry about the comments i'll try to stop using them as much (only that it is a good record of what i thought/found out about something when i wrote the program.)
    Last edited by tzpb8; 07-26-2006 at 11:04 AM.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >confirmed, seems arrays can hold different types of pointers
    But not all at the same time. Arrays are homogenous data types. You can subvert the type system to mix and match, but it's not a good idea.

    >//now to find a flexible/easy way to print out the content
    >//of each memory address in the array of pointers
    You're not using an array of pointers. You're using an array of int and mangling the types of the data you put in it. This is an array of pointers:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      int a = 10, b = 20, c = 30;
      int *array[3] = {&a, &b, &c};
      int i;
    
      for ( i = 0; i < 3; i++ )
        printf ( "%d\n", *array[i] );
    
      return 0;
    }
    >Sorry about the comments
    The comments aren't a big problem. Your code is practically unreadable. A good formatting technique makes writing correct code so much easier.
    My best code is written with the delete key.

  13. #13
    1 == 1 tzpb8's Avatar
    Join Date
    Jul 2006
    Posts
    36
    Ooh that i haven't tried before "int *array[3] = {&a,&b,&c}" nice
    I noticed you use both "(void)" and "return 0" is that for any reason?

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I noticed you use both "(void)" and "return 0" is that for any reason?
    Because it's correct. A good way to learn proper C (standard, well formatted code) is to look at the code posted by most of this forum's senior members. If you hang around here long enough, you get tired of constantly being flamed for bad code, and correct your ways.
    My best code is written with the delete key.

  15. #15
    1 == 1 tzpb8's Avatar
    Join Date
    Jul 2006
    Posts
    36
    You mean the array i wrote isn't really a pointer array, cause the pointers just points to a int/char/float/etc.. variable and not to "void"? so the array cant be treated as a pointer array, ok think i got it thx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. char pointers and their addresses.
    By ens_leader in forum C Programming
    Replies: 5
    Last Post: 12-31-2007, 01:48 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM
  5. Pointers and their Addresses
    By incognito in forum C++ Programming
    Replies: 6
    Last Post: 12-29-2001, 07:16 PM