Solved

Increment pointer

Posted on 2004-04-26
14
521 Views
Last Modified: 2012-06-27
How do you increment a pointer (both the pointer and the content of the pointer)?

thnx
krithi
0
Comment
Question by:krithi
  • 3
  • 3
  • 2
  • +3
14 Comments
 
LVL 12

Expert Comment

by:stefan73
ID: 10916628
Hi krithi,
Both are simple:

char* myptr = "Hello, World!";

main(){
    printf("Original string: %s\n",myptr);
    (*myptr)++;
    printf("After increase content: %s\n",myptr);
    myptr++;
    printf("After increase pointer: %s\n",myptr);
}

Cheers,
Stefan
0
 
LVL 7

Expert Comment

by:vijay_visana
ID: 10916690
I think above code wan't run (I compiled with M$ VC 6)

(*myptr)++;//<- You will get Access violation exception here
0
 

Author Comment

by:krithi
ID: 10916753
how abt this guys is this correct?

int *pFirst;
int nFirst = 0, nSize = 3;

pFirst = &nFirst;      // Address of nFirst;
*pFirst = 1;
for (nFirst = 0; nFirst < nSize; nFirst ++)
{
      *(pFirst + 1) = *pFirst + 1; //increments both the pointer and the pointer value
}
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 7

Expert Comment

by:vijay_visana
ID: 10916845
this is also wrong
and it will give you error.

You need some allocated memory to keep your pointer, same as with value you need some variable to keep value.
you have declared int* pFirst means you have reserved on location to store one pointer only.
but when you do
pFirst+1 this location is not reserved by you hence access violation

As you need array to keep multiple value you need array to keep pointers too.
Pointer is also value that need some preallocated/predeclared storage.
one can not just incremennt pointer hope that compiler will be smart enough to understad your requirement.



Vijay
0
 
LVL 11

Expert Comment

by:lbertacco
ID: 10916853
Well, to increment both pointer and pointed values, you can just do:

(*ptr++)++;

since the ++ is postfix, it will increment *ptr and ptr.

But it looks like you are trying to do something different. I'm not sure what you want to do but your code, inside the for loop, tries to successively assign values nFirst+1 (that is 1,2,3) to address  &nFirst+1, which seems to be unallocated.
Also when you set *pFirst=1, you are setting the value of variable nFirst to 1 (since pFirst==&nFirst), but immediately after this you set again nFirst to 0 (in the for loop) which doesn't make much sense.


0
 
LVL 12

Expert Comment

by:stefan73
ID: 10917207
vijay_visana,
> (*myptr)++;//<- You will get Access violation exception here

Yes, you're right. The string constants are read-only.

Stefan
0
 
LVL 12

Accepted Solution

by:
stefan73 earned 50 total points
ID: 10917213
Try this instead:

char  helloMem[]="Hello, World!";
char* myptr = helloMem;

main(){
    printf("Original string: %s\n",myptr);
    (*myptr)++;
    printf("After increase content: %s\n",myptr);
    myptr++;
    printf("After increase pointer: %s\n",myptr);
}

...should work.
0
 
LVL 7

Expert Comment

by:vijay_visana
ID: 10917253
will work
0
 
LVL 5

Expert Comment

by:dennis_george
ID: 10917274
Hi,

Hi I think you want both the pointer and the value incremented .......

See ++ operator is used for increment and when you apply this operator to a pointer then the pointer will increment and when you apply it on a value then the value will be incremented.......l


I will show you an example in which I am incrementing both value and pointer at the same time......

#define SIZE 5

int main()
{
      int iaNum[SIZE] = {0} ; // Array of size 5 all intialized to 0
      int *pPt = iaNum ; // A pointer pointing to the start of the integer array iaNum

      ++pPtr ; // Inrement the pointer once so that it will point to the 2nd index of array i.e. iaNum[1]

      for (int i = 0; i < SIZE - 1; ++i) {
            *(pPtr++) = ++(*(pPtr-1)) ;
//     RHS :: *(pPtr-1) ::  get the value at the previous location pointed by the pointer
//                  ++(*(pPtr-1)) :: Now increment that value
//      LHS :: pPtr++ increment the pointer by one position
//               *(pPtr++) and get the value at that position
       }

      for (i = 0; i < SIZE; i++) {
            cout << endl << iaNum[i]  ;
      }

      cout << endl ;

      return 0 ;
}


Dennis
0
 
LVL 1

Expert Comment

by:farrooda
ID: 10917339
to increment a pointer's content you could use (*ptr)++;
but in order to increment a pointer's adress you should have declared a pointer to array
for example: int ptr*=int array[5];
here you could increment the ptr:
ptr points first to array[0]
ptr++;
ptr points now to array[1];
until you reach array[4];
0
 
LVL 1

Expert Comment

by:farrooda
ID: 10917350
but if you do ptr++ after , the program will generate a runtime error since you are pointing to a memory location which was reserved for something else;
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question