pointers

This is a discussion on pointers within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> main(){ int a[5]={5,50,100,150}; int *iPtr; iPtr=a; printf("\n%d", ++*iPtr); // this will increment a[0] but not temporary printf("\n%d", *iPtr+4); ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    94

    pointers

    Code:
    #include<stdio.h>
    main(){
    	int a[5]={5,50,100,150};
    	int *iPtr;
    	iPtr=a;
    	printf("\n%d", ++*iPtr); // this will increment a[0] but not temporary
    printf("\n%d", *iPtr+4); // this will increment a[0] temporary
    could you please show me how to increment a[0] for 4 (+4) but while using pointers as above?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,318
    As in you want to write the expression a[0] + 4 using pointer notation rather than array index notation?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The easiest way, provided your pointer is valid would be *ptr++ or ++*ptr.

    You can also do math this way ... *ptr = 8 * 3;

    When defining a pointer as in int *ptr The star says "I need an address here"
    When manipulating values at a pointer as in *ptr The star says "Do this at the given address".

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Take note the difference between (*ptr)++ and *ptr++.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    94
    Code:
    printf("\n%d", *iPtr+4); // this will increment a[0] temporary
    guys I am asking how to increment as I do above but this is temporary and I would like my incrementation to not be temporary!!!

    btw. I understand this

    Quote Originally Posted by Bayint Naung View Post
    Take note the difference between (*ptr)++ and *ptr++.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,318
    Quote Originally Posted by c_lady
    I am asking how to increment as I do above but this is temporary and I would like my incrementation to not be temporary!!!
    Oh, then you should write:
    Code:
    a[0] += 4;
    or:
    Code:
    *iPtr += 4;
    But then you should already know this before starting on arrays and pointers.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    Code:
    #include <stdio.h>
    int main(){
    
      int *ptr, a[5] = {0,0,0,0,0}; 
    
      ptr = a; // ptr points to a
      *ptr += 4; // Increase the value which ptr points to by 4, in this case a[0]+4
    
      *(++ptr) += 5; // This should increment a[1] by 5...  notice the use of () and ++
    
      printf("a[0]= %d\n", a[0]);
      printf("a[1]= %d\n", a[1]);
    
      return 0;
    }
    Output:

    Code:
    $ gcc test.c
    $ ~/a.out
    a[0]= 4
    a[1]= 5
    edit:

    May not be needed to use () but I like to because in longer expressions things can get very complicated and C's order of operation isn't always clear.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays with pointers
    By Niels_M in forum C Programming
    Replies: 18
    Last Post: 07-31-2010, 03:31 AM
  2. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  3. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  4. pointers to arrays
    By rakeshkool27 in forum C Programming
    Replies: 1
    Last Post: 01-24-2010, 06:28 AM
  5. Pointers pointers pointers....
    By G'n'R in forum C Programming
    Replies: 11
    Last Post: 11-02-2001, 01:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21