Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Increment pointer

Posted on 2004-04-26
14
Medium Priority
?
533 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

972 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