Thread: need help with assigning memory and changing memory addresses

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    79

    need help with assigning memory and changing memory addresses

    Hey i was watching some stanford lectures on youtube and i like the bit about working with memory. So i figured i would try and do something like passing the address of an array to a function and manually going through the memory and overiding whats in the memory at a certain point to change the array, but im getting confused

    Code:
    #include <stdio.h>
    
    int changearray(int *arr){
    //pass the reference to the first element of array
    
    //*arr should now point to the first elemet of the array?  
    //the above doesnt seem to be true but i thought thats what would happen?
    
    //move two elements down the array
    
    int *finaladdress = arr + sizeof(int)*4;
    
    //above im trying to assign finaladdress to point to the 4th element of the passed array
    //again this doesnt seem to be working i must be confused. 
    
    //now i want to change the 4th element of the array
    finaladdress = 1;
    
    return 0;
    
    }
    
    int main(){
    
    int arr[10];
    changearray(&arr);
    
    return 0;
    
    
    }
    i just wrote this quickly and isnt exactly the same code, but it was the same method i was using, i just want to understand how to pass an address then in a seperate function assign a new value to that address.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You don't want sizeof when doing pointer arithmetic. You automatically move down in size-of-thing-being-pointed-at chunks, so if you want to move to the fourth element you would do
    Code:
    int *finaladdress = arr+3;
    EDIT: And &arr is not what you want to pass. You want to pass "arr".

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Try this:
    Code:
    #include <stdio.h>
    
    void changearray(int *arr){
    //pass the reference to the first element of array
    
    //*arr should now point to the first elemet of the array?  
    //the above doesnt seem to be true but i thought thats what would happen?
    
    //move two elements down the array
    
    int *finaladdress = arr + 4;
    
    //above im trying to assign finaladdress to point to the 4th element of the passed array
    //again this doesnt seem to be working i must be confused. 
    
    //now i want to change the 4th element of the array
    *finaladdress = 1;
    }
    
    int main(){
    int i;
    int arr[10]= { 0 };
    changearray(arr);
    printf("\n\n");
    for(i = 0; i < 10; i++)
      printf("\nElement %d: %d", i, arr[i]);
    
    return 0;
    }
    Adjust the +'s as needed.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    EDIT: And &arr is not what you want to pass. You want to pass "arr".
    I believe for a stack array they are equivalent. The address of arr IS arr (arr = &arr = &arr[0]).

    Obviously that's not true for dynamic arrays, since &ptr would be the address of the pointer.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It has the same value, but the wrong type. And since of course people are compiling with warnings turned on high, we might as well get the type right.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    //now i want to change the 4th element of the array
    finaladdress = 1;
    use

    Code:
    *finaladdress = 1;
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    79
    ok thanks for your help. Something im confused about.

    when i want to set the address of finaladdress i am doing

    int *finaladdress = arr + 4;

    i assume the address of finaddress is the same as the 4th element of the array so doing this we have changed the address of final address.

    then to change the value

    *finaladdress = 1 ;

    wouldnt this just change the address again?

    my vision of how things work are

    *finaladdress = //this is basically changing the address finaladdress is stored in
    finaladdress = // this stores the value to be associated with that address??

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    79
    also when changing the address we do arr + 4 do we only do this because the compiler reconises its an int and thus 4 bytes long?

    if we were using a void * would we have to use the sizeof(int) to calculate the address?

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    79
    sorry another question
    when i do this

    int *finaladdress = arr + 4;
    printf("The address of finaladdress: %p", &finaladdress);

    it does not equal the same address as any elements of the array.

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    int *finaladdress = arr + 4;
    is the same as
    Code:
    int *finaladdress;
    finaladdress = arr + 4;
    This changes value of the pointer

    Code:
    *finaladdress = 4;
    or

    Code:
    finaladdress[0] = 4;
    This changes pointed memory

    To change the 4th element of the array you do not need a separate pointer to it. Just

    Code:
    arr[3] = 4;
    will do the work
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by mushy View Post
    sorry another question
    when i do this

    Code:
    int *finaladdress = arr + 4;
    printf("The address of finaladdress: %p", &finaladdress);
    it does not equal the same address as any elements of the array.
    You are printing address of the variable finaladdress

    Each variable has its own address, even if this variable is a pointer, storing address of another variable

    Code:
    printf("The address stored in finaladdress: %p", (void*)finaladdress);
    Will print the address stored inside the finaladdress variable, it will be equal to the address of arr[4]
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    79
    ok, so pointers all have a unique memory address and their content is just the address of what its pointing too?

    if thats the case why do we use pointers? isnt that the way a normal variable works? it has its own address and it contains a value?

    sorry for all the questions, im just confused about this.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes, a pointer contains an address, (what it's pointing to).

    Pointers have a greater utility than any other variable. Computers rely on memory addresses, so pointers are a natural thing to have and use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. changing protected memory
    By mathewmefiu in forum C++ Programming
    Replies: 10
    Last Post: 02-09-2010, 11:24 PM
  2. Changing memory address
    By tzpb8 in forum C Programming
    Replies: 3
    Last Post: 07-26-2006, 09:50 AM
  3. Memory is changing on me!
    By durban in forum C++ Programming
    Replies: 8
    Last Post: 10-13-2005, 09:33 AM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM