You are not logged in. Please login at www.codechef.com to post your questions!

×

weird behaviour of scanf

#include<stdio.h>
#include<conio.h>
void array(int * ,int );


int main()
{
    int n,a[20];
    printf("Enter the size of the array");
    scanf("%d",&n);
    array(&a[0],n);
}
void array(int *j,int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        printf("\n Enter the elements of array");
            scanf("%d",j);
                        j++;
    }
    printf("The elements of array are");
    for(i=0;i<n;i++)
    {
        printf("\n%d",*(j));
                j++;



    }
}

IN #scanf# IF IN PLACE OF THAT I AM DOING THIS THEN THE CODE IS SUCCESSFULLY EXECUTING. CAN ANYONE EXPLAINN ME WHY?? though both means same..

scanf("%d",(j+i)); and deleting j++;

asked 30 Apr '14, 11:25

coder1901's gravatar image

1★coder1901
111
accept rate: 0%

edited 30 Apr '14, 13:23

kuruma's gravatar image

3★kuruma
17.7k72143209

1

you are increasing j but not shifting back to initial position. Use j+i.

(02 May '14, 01:59) goodboy2★

hello coder 1901,

I see you are new to programming, couple of mistakes that I spotted

1.void array(int j,int n) -> j should be a int pointer and hence int*

2.scanf("%d",j); add &

  1. please format and repost so that it is easy to analyse for us viewers,,

not sure about your question,,

good day!!!

link

answered 30 Apr '14, 11:41

sdinesh94's gravatar image

0★sdinesh94
-7
accept rate: 0%

ok, i found it,, your j++ has local scope meaning for every iteration of for loop you are going to start off again with &a[0]. Hope this helps

(01 May '14, 00:56) sdinesh940★

Hi,

Since j is a pointer to an array, it means that j holds the first value in the array.

This means that incrementing it will produce the same effect as that of iterating trough the array elements, i.e., if *j is the first element of the array, then, doing j++, or, as you wrote in your code j+i, will change the pointer's location to point to the ith element in the array, and, as such, both notations will produce the same effect :)

Best,

Bruno

link

answered 30 Apr '14, 13:29

kuruma's gravatar image

3★kuruma
17.7k72143209
accept rate: 8%

array(&a[0],n); i think is wrong you should write array(a,n);...... because arrays are pointers

Thank you.

link

answered 01 May '14, 13:37

fahd_511's gravatar image

1★fahd_511
1
accept rate: 0%

After the first for loop, j points to index a + n. The next for loop starts incrementing j from that location. To make the same code print the array values correctly, either change the scanf argument to a + i or make the following change:


void array(int j,int n)
{
    int i;
    int k = j;
    for(i=0;i < n;i++)
    {
        printf("\n Enter the elements of array");
            scanf("%d",j);
                        j++;
    }

j = k;
printf("The elements of array are");
for(i=0;i < n;i++)
{
    printf("\n%d",*(j));
            j++;



}

}

link

answered 02 May '14, 14:36

bbackspace's gravatar image

4★bbackspace
9614
accept rate: 6%

edited 02 May '14, 14:37

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×62

question asked: 30 Apr '14, 11:25

question was seen: 930 times

last updated: 02 May '14, 14:37