pointer coding

This is a discussion on pointer coding within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <conio.h> void fun(int , int*); int main(void){ int a, i; int x = 100,y = 200, ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    18

    pointer coding

    Code:
    #include <stdio.h>
    #include <conio.h>
    void fun(int , int*);
    int main(void){
    int a, i;
    int x = 100,y = 200, z = 300;
    int myArr[] = {18,22,3};
    int *ptr1, *ptr2;
    ptr1 = &y;
    ptr2 = &z;
    printf("%d\n",*ptr1 + x);
    a = *ptr2;
    ptr2 = &x;
    printf("%d %d\n",a, *ptr2);
    fun(a, myArr);
    ptr1 = myArr;
    printf("%d\n",*ptr1++);
    ptr1--;
    printf("%d\n",--(*ptr1));
    getch();
    return 0;
    }
    void fun(int b , int* pr){
    int *p;
    for(p = pr; p < pr+3; p++)
    *p = *p + b;
    printf("%d\n",*--p);
    }
    For the coding above, what will *p be? what is the value? I have problems tracing this coding..

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    Compile and test. You can also use a debugger.
    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
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > I have problems tracing this coding..
    Perhaps some indentation, and the occasional blank line to space things out would help.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  2. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. How did you master pointers?
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-17-2006, 07:23 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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