Thread: array and pointer problem

  1. #1
    Registered User
    Join Date
    Oct 2016

    array and pointer problem

    //c program

    #include <stdio.h>
    int main(void) {
        static int arr[] = {97, 98, 99, 100, 101, 102, 103, 104 };
         int * ptr = arr + 1;
          print(++ptr, ptr--, ptr, ptr++, ++ptr);
          return 0;
    int print(int *a, int *b, int *c, int *d, int *e)
       printf("%d %d %d %d %d \n", *a, *b, *c, *d, *e);
    Expected Output of program by me:
    99 99 98 98 100

    Actual Output of program:
    100 100 100 99 100

    Could you please explain the Actual output?

  2. #2
    Registered User
    Join Date
    Jun 2015
    Two problems:
    1. In C there's no guarantee about the order in which a function's arguments are evaluated.
    2. You're not allowed to modify the same variable more than once per "sequence point". Question 3.8

    Compiling your program with gcc and full warnings gives multiple "sequence point" warnings. (You also forgot to prototype your print function and are not returning the promised int.)

    Although an explanation for the exact values you received could be garnered from the assembly code, it would be essentially meaningless since this is undefined behaviour.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Char Pointer Array cin Problem
    By yurci in forum C++ Programming
    Replies: 8
    Last Post: 11-03-2012, 05:21 PM
  2. problem with pointer in array
    By hugoguan in forum C Programming
    Replies: 7
    Last Post: 11-30-2010, 02:13 AM
  3. Pointer array problem
    By ernielam1024 in forum C Programming
    Replies: 10
    Last Post: 06-17-2006, 09:26 AM
  4. Pointer&Array problem
    By LloydUzari in forum C++ Programming
    Replies: 4
    Last Post: 08-15-2004, 02:18 PM
  5. problem with pointer/array
    By razza in forum C Programming
    Replies: 9
    Last Post: 05-22-2002, 12:20 PM

Tags for this Thread