problem with pointer to array

A

ankitjain.bvcoe

Hi ,
i don't know why the below mentioned program is giving wrong o/p.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
ARRAY_SIZE 10
int func(void *x)
{
int (*y)[ARRAY_SIZE];
y=x;
printf("%d, %d",y[0],y[1]);
return 0;}

int main (void)
{
int arr[ARRAY_SIZE]={1,2};
int (*p)[ARRAY_SIZE];
p=&arr;
func(p);
return 0;
}


O/P -10272722,-18928292
 
K

Keith Thompson

i don't know why the below mentioned program is giving wrong o/p.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

You don't use anything in either said:
ARRAY_SIZE 10

I presume this was supposed to be "#define ARRAY_SIZE 10".
int func(void *x)
{
int (*y)[ARRAY_SIZE];
y=x;
printf("%d, %d",y[0],y[1]);
return 0;}

y is a pointer to an array of 10 ints. y[0] is an array of 10 ints,
which decays to a pointer to an array of 10 ints. You print this
pointer value with a "%d" format, which expects an int, not a pointer.
(I'm not 100% certain I've gotten all the details right.) Undefined
behavior.

Also, you should print '\n' character at the end of your output;
otherwise it's not guaranteed that your output will appear.
int main (void)
{
int arr[ARRAY_SIZE]={1,2};
int (*p)[ARRAY_SIZE];
p=&arr;
func(p);
return 0;
}


O/P -10272722,-18928292

It's rarely makes sense to declare pointers to arrays. Usually you
want a pointer to the element type; you can use such a pointer to
access the rest of the array. You can pass the length of the array as
a separate parameter.

Here's a more straightforward way to do what you're trying to do:

#include <stdio.h>

#define ARRAY_SIZE 10

int func(int *arr)
{
printf("%d, %d\n", arr[0], arr[1]);
return 0;
}

int main(void)
{
int arr[ARRAY_SIZE] = {1,2};
func(arr);
return 0;
}

Output:

1, 2
 
M

Markus Moll

Hi

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
ARRAY_SIZE 10

That won't compile. You probably meant "#define ARRAY_SIZE 10"
int func(void *x)
{
int (*y)[ARRAY_SIZE];
y=x;
printf("%d, %d",y[0],y[1]);

Well, think about it: y is a pointer to array of int of size ARRAY_SIZE.
Then y[0] is equivalent to (*y), which is an array of int.
The pointer y does not refer to an array of anything, so y[1] doesn't make
any sense.
To get the first and second entry of _*y_, you have to write (*y)[0] or (*y
[1], respectively.


Markus
 
S

santosh

#include said:
#include <stdlib.h>
#include <string.h>
ARRAY_SIZE 10
int func(void *x)
{
int (*y)[ARRAY_SIZE];
y=x;
printf("%d, %d",y[0],y[1]);
return 0;}

int (*y)[ARRAY_SIZE]; is not required.
int *y = (int*)x ; is more than enough.
 
A

al3x4nder

santosh напиÑав:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
ARRAY_SIZE 10
int func(void *x)
{
int (*y)[ARRAY_SIZE];
y=x;
printf("%d, %d",y[0],y[1]);
return 0;}

int (*y)[ARRAY_SIZE]; is not required.
int *y = (int*)x ; is more than enough.

in context func(), IMHO, int *y = (int*)x ; s not required as well,
just
change type of pointer in function proto to int * x, and them
using it as x[0], x[1], that eq *(x + 0), *(x + 1).
or i wrong?
 
M

Markus Moll

Hi
int (*y)[ARRAY_SIZE]; is not required.
int *y = (int*)x ; is more than enough.

Well... required... enough...

If you want to pass (statically sized) arrays of ARRAY_SIZE ints, then I
think it's best to use int (*y)[ARRAY_SIZE], as this will probably giva a
type error warning if you pass a differently sized array.

Markus
 
A

ankitjain.bvcoe

thanks for your reply .

there r still some confusions ....

1) here y is a pointer to an array .
let's say array is

contents ----- >1 2 3 4
5
address ------>2000 2004 2008 2012 2016

then y is pointer to this array therefore
contents of y =2000
add. of y = anything
this is what i have checked
So, now *y should be 1
but its not,so why its not like this ..

2) As per your comments..
You don't use anything in either <stdlib.h> or <string.h>.

Actually this was just a rough sample program related to my prj.So i
just created
a snapshot of the problem faced .
 
M

Markus Moll

Hi

there r still some confusions ....

1) here y is a pointer to an array .
let's say array is

contents ----- >1 2 3 4
5
address ------>2000 2004 2008 2012 2016

then y is pointer to this array therefore
contents of y =2000

Why that? (It probably "is", but: y does _not_ point to the first element of
the array, but rather to the array itself. This means that what you get by
dereferencing y is an array, not an element of the array.)
So, now *y should be 1

No, *y should be an array, which is converted to a pointer to the first
element.
but its not,so why its not like this ..

Because you ignore types in your reasoning.
Actually this was just a rough sample program related to my prj.So i
just created
a snapshot of the problem faced .

Yes, but you should really make your examples minimal. Including headers
that aren't needed confuses the reader, because he tries to find out why
they were included.

Markus
 
I

Ichigo Kuler

Try to use (int (*x)[array_size]) to replace the ( void *x)
maybe,it is more complex than yours, but it is working well and easy to
understand.
 
S

stathis gotsis

Hi ,
i don't know why the below mentioned program is giving wrong o/p.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
ARRAY_SIZE 10
int func(void *x)
{
int (*y)[ARRAY_SIZE];
y=x;
printf("%d, %d",y[0],y[1]);
return 0;}

int main (void)
{
int arr[ARRAY_SIZE]={1,2};
int (*p)[ARRAY_SIZE];
p=&arr;
func(p);
return 0;
}

I do not understand why you need to do it this way, but this compiles
without warnings:

#include <stdio.h>

#define ARRAY_SIZE 10

int func(void *x)
{
int (*y)[ARRAY_SIZE];
y=x;
printf("%d, %d\n",y[0][0],y[0][1]);
return 0;}

int main (void)
{
int arr[ARRAY_SIZE]={1,2};
int (*p)[ARRAY_SIZE];
p=&arr;
func(p);

return 0;
}
 
A

aisman

(e-mail address removed) napisal(a):
Hi ,
i don't know why the below mentioned program is giving wrong o/p.


#include <stdio.h>
#include <stdlib.h>> #include <string.h>
ARRAY_SIZE 10
int func(void *x)
{
int (*y)[ARRAY_SIZE];
y=x;
printf("%d, %d",y[0],y[1]);
return 0;}

int main (void)
{
int arr[ARRAY_SIZE]={1,2};
int (*p)[ARRAY_SIZE];
p=&arr;
func(p);
return 0;
}


O/P -10272722,-18928292

This is right:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
ARRAY_SIZE 10
int func(void *x)
{
int *y;
y=(int *)x;
printf("%d, %d",y[0],y[1]);
return 0;}

int main (void)
{
int arr[ARRAY_SIZE]={1,2};
int *p;
p=arr;
func(p);
return 0;
}


or:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ARRAY_SIZE 10
int func(void *x)
{
int (*y)[ARRAY_SIZE];
y=&x;
printf("%d, %d",y[0],y[1]);
return 0;}

int main (void)
{
int arr[ARRAY_SIZE]={1,2};
int (*p)[ARRAY_SIZE];
p=&arr;
func(*p);
return 0;
}
 
E

Emmanuel Delahaye

(e-mail address removed) a écrit :
ARRAY_SIZE 10

Missing #define. Please don't retype, but copy & paste...

#define ARRAY_SIZE 10
int func(void *x)
{
int (*y)[ARRAY_SIZE];
y=x;
printf("%d, %d",y[0],y[1]);

pointers want dereferencment

printf("%d, %d",*y[0],*y[1]);
 
C

Chris Torek

i don't know why the below mentioned program is giving wrong o/p.

[snip code with syntax error - obviously this was not the actual
code!]

You are using the type "pointer to array N of T", for some appropriate
integer constant N and element-type T. Such a pointer can (and in
your case does) point to the first of several such arrays:

int (*p)[10];
int arr[4][10];

p = &arr[0];

Now p points to the first of 4 arrays, i.e., arr[0]. Each of the
four arrays is itself an array of size 10 of "int".

This is a generalization of the fact that a pointer of type
"pointer to T" (for any valid element type T) can point to
the first of many objects of type T. This is why, for instance,
we can do:

char buf[SIZE];
char *s = &buf[0];

Now "s" points to the first element of buf, but s, for any
valid integer i, names the array element buf.

Note that the pointer "s" can point to just *one* item:

char tmp;
...
s = &tmp;

after which only s[0] (which is the same as "*s") is valid. It
can even point to *no* items at all:

s = NULL;

after which even s[0] is invalid.

Given any pointer value "v", of type "pointer to T", there are
several questions you need to answer to your own satisfaction --
not necessarily with code, maybe just by *thinking* -- before using
that value:

- Is the value valid at all? (I.e., if "v" is a variable, has it
been initialized? If it contains garbage, do not use it; just
overwrite it with a valid value.)

- Is it NULL?

- If it is not null, does it point to an actual object, or the
first of many objects (or perhaps even into the middle of a
set of objects)?

- If it points to the first (or middle) of a set of objects,
how many objects are there around that location? (And how
do you know?)

See also <http://web.torek.net/torek/c/pa.html>.
 
B

Barry Schwarz

Hi

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
ARRAY_SIZE 10

That won't compile. You probably meant "#define ARRAY_SIZE 10"
int func(void *x)
{
int (*y)[ARRAY_SIZE];
y=x;
printf("%d, %d",y[0],y[1]);

Well, think about it: y is a pointer to array of int of size ARRAY_SIZE.
Then y[0] is equivalent to (*y), which is an array of int.
The pointer y does not refer to an array of anything, so y[1] doesn't make
any sense.
To get the first and second entry of _*y_, you have to write (*y)[0] or (*y
[1], respectively.

Or the equivalent but more common y[0][0] and y[0][1].


Remove del for email
 
B

Barry Schwarz

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
ARRAY_SIZE 10
int func(void *x)
{
int (*y)[ARRAY_SIZE];
y=x;
printf("%d, %d",y[0],y[1]);
return 0;}

int (*y)[ARRAY_SIZE]; is not required.
int *y = (int*)x ; is more than enough.

Since x is a void*, the cast is unnecessary.


Remove del for email
 
A

aisman

I made mistake:

This is good


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ARRAY_SIZE 10
int func(void *x)
{
int (*y)[ARRAY_SIZE];
y=x;
printf("%d, %d",*(*y+0),*(*y+1));
return 0;}

int main (void)
{
int arr[ARRAY_SIZE]={1,2};
int (*p)[ARRAY_SIZE];
p=&arr;
func(*p);
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top