Solved

Increment pointer

Posted on 2004-04-26
14
525 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

630 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