Solved

Increment pointer

Posted on 2004-04-26
14
519 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now