Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Increment pointer

Posted on 2004-04-26
14
Medium Priority
?
529 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 150 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

722 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