locked
base addr of ptrs RRS feed

  • Question

  • can we increment the base addr of a ptr ....
    i hope yes but when i ysed a++ it showed error ...why is that so
    Saturday, September 15, 2007 4:40 AM

Answers

  • you can increment base adrr but you cannot modify and restore


    i.e
    when you use  *(a+1) the value in the baseaddr+1 th location is retrived

    but when you use a++ the base addr is incremented and again it is stored into a i.e trying to modify base addr is not allowed



    cheers
    nana
    Saturday, September 15, 2007 4:48 AM

All replies

  • you can increment base adrr but you cannot modify and restore


    i.e
    when you use  *(a+1) the value in the baseaddr+1 th location is retrived

    but when you use a++ the base addr is incremented and again it is stored into a i.e trying to modify base addr is not allowed



    cheers
    nana
    Saturday, September 15, 2007 4:48 AM
  • int *p;
    int a=10;
    p = &a ;
    p++;


    this is perfectly valid man, though it serves no purpose. here is a useful example that will illustrate the thing.

    int *p;
    int arr[10]={1,2,3,4,5,6,7,8,9,10};
    int i;
    p=&a;
    for(i=0 ; i<10 ' i++) {
    printf("%d", *p);
    p++;    // point to the next element in the array.
    }
    Monday, September 17, 2007 5:36 PM
  • Yes, if you declare a pointer variable, its address can always be changed.....
    But if you declare a array and try to change that base address, it wont be allowed....

    You can also try this
    int *ptr = new int[10];

    now you can also do ptr++;
    But remember, you have a chance of loosing the address of this new memory location, thus it is not advisable.....
    Monday, September 17, 2007 11:06 PM