473,396 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

problem with pointer to array



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

Feb 17 '06 #1
15 1339
"an*************@gmail.com" <an*************@gmail.com> writes:
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 <stdlib.h> or <string.h>.
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

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 17 '06 #2
Hi

an*************@gmail.com wrote:
#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

Feb 17 '06 #3
> #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.

Feb 17 '06 #4

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?

Feb 17 '06 #5
Hi

santosh wrote:
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
Feb 17 '06 #6
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 .

Feb 17 '06 #7
Hi

an*************@gmail.com wrote:
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

Feb 17 '06 #8
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.

Feb 17 '06 #9
<an*************@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...


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;
}
Feb 17 '06 #10
an*************@gmail.com 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;
}

Feb 17 '06 #11
In article <11*********************@o13g2000cwo.googlegroups. com>
an*************@gmail.com <an*************@gmail.com> wrote:
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[i], for any
valid integer i, names the array element buf[i].

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>.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Feb 17 '06 #12
On Fri, 17 Feb 2006 10:30:11 +0100, Markus Moll
<mo**@rbg.informatik.tu-darmstadt.de> wrote:
Hi

an*************@gmail.com wrote:
#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
Feb 18 '06 #13
On 17 Feb 2006 02:00:30 -0800, "santosh" <sa***********@gmail.com>
wrote:
#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
Feb 18 '06 #14
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;
}

Feb 18 '06 #15
an*************@gmail.com 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]);

--
A+

Emmanuel Delahaye
Feb 18 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tony Johansson | last post by:
Hello Experts!! I have two small classes called Intvektor and Matris shown below and a main. Class Intvektor will create a one dimension array of integer by allocate memory dynamically as you...
3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
28
by: Davy | last post by:
Hi all, I found char x={"my"}; can be compiled. But char x; x={"my"}; can not be compiled.
6
by: Edd Dawson | last post by:
Hi. I have a strange problem involving the passing of command line arguments to a C program I'm writing. I tried posting this in comp.programming yesterday but someone kindly suggested that I'd...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
3
by: Wild Wind | last post by:
Hello, I made a post relating to this issue a while back, but I haven't received any answer, so here I am again. I am writing a mixed C++ dll which uses the following declaration: typedef...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
39
by: Martin Jrgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
22
by: sam_cit | last post by:
Hi Everyone, I have the following structure in my program struct sample { char *string; int string_len; };
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.