将指针初始化为数组 [英] Initializing Pointer to an array

查看:111
本文介绍了将指针初始化为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我假设以下事情。

1.Pointer到一个整数意味着它指向一个整数,在递增

指针时,它指向下一个整数在内存中。

2.Pointer到一个大小的数组意味着它指向一个数组,在
递增指针时,它应指向该大小的下一个数组。


如果我错了,请纠正我。

我们声明一个指向大小为N的整数数组的指针

int(* a)[N];


现在我的问题是

我有一个100个整数的数组,指向10个整数数组的指针

int(* a)[10];

int b [100];

如何初始化指针a指向数组b base

地址。

I am assuming the following things.
1.Pointer to an integer means it points to an integer,On incrementing
the pointer,it points to the next integer in memory.
2.Pointer to an array of some size means it points to an array,On
incrementing the pointer,it should point to next array of that size.

Correct me if i am wrong.
We declare a pointer to an integer array of Size N as
int (*a)[N];

Now My Problem is
I have an array of 100 Integers,Pointer to an array of 10 integers
int (*a)[10];
int b[100];
How can i initialize the pointer a to point to the array b base
address.

推荐答案

我能够将指向数组的指针初始化为

int(* a)[10];

int b [100];


a =& b [0];

但我收到警告

警告:分配来自不兼容的指针类型


我的代码是

int main()

{

int( * a)[10];

int b [100];

int i;


for(i = 0; i< 100; i ++)

b [i] = i;


a =& b [0];


printf(" %u%u%d%d \ n",a,& b [0],** a,b [0]);

a ++;

printf("%u%u%d%d \ n",a,& b [10],** a,b [10]);

a ++;

printf("%u%u%d%d \ n",a,& b [20],** a,b [20]);

a ++;

printf("%u%u%d%d \ n",a,& b [30],** a,b [30]);

}


我得到了正确的结果

3221219184 3221219184 0 0

3221219224 3221219224 10 10

3221219264 3221219264 20 20

3221219304 3221219304 30 30

I am able to initialize the pointer to the array as
int (*a)[10];
int b[100];

a = &b[0];
But i am getting the warning
warning: assignment from incompatible pointer type

My Code is
int main()
{
int (*a)[10];
int b[100];
int i;

for(i=0;i<100;i++)
b[i]=i;

a = &b[0];

printf(" %u %u %d %d\n",a ,&b[0],**a,b[0]);
a++;
printf(" %u %u %d %d\n",a ,&b[10],**a,b[10]);
a++;
printf(" %u %u %d %d\n",a , &b[20],**a,b[20]);
a++;
printf(" %u %u %d %d\n",a , &b[30],**a,b[30]);
}

I am getting correct results as
3221219184 3221219184 0 0
3221219224 3221219224 10 10
3221219264 3221219264 20 20
3221219304 3221219304 30 30


ru ******** @ rediffmail.com 写道:
ru********@rediffmail.com wrote:
我能初始化指向数组为
int(* a)[10];
int b [100];
a =& b [0];
但是我收到警告
警告:从不兼容的指针类型分配


当然你是。 a是指向10个int的数组的指针(你可能不需要
),而& b [0]是指向int的指针。你的

编译器帮你一个忙。

我的代码是
int main()


int main( void)/ *更好* /

{
int(* a)[10];
int b [100];
int i;
for(i = 0; i< 100; i ++)


for(i = 0; i< sizeof b; i ++)/ * better * /

b [i] = i;
a =& b [0];


错误,正如我所说。

printf("%u%u%d%d \ n,a,& b [0 ],** A,b [0]);


错误多种方式:


1)您没有包含< stdio.h> ;.

2)%u不是指针所需的转换说明符。


printf("%p%p%d%d \ n",(void *)a,( void *)& b [0],** a,b [0]);


注意演员阵容;它们是必需的。

a ++;
printf("%u%u%d%d \ nn,a,& b [10],** a,b [ 10]);
a ++;
printf("%u%u%d%d \ n,a,& b [20],** a,b [20]);
a ++;
printf(%u%u%d%d \ n,a,& b [30],** a,b [30]);


除非您使用的是C99编译器,否则必须从

main()返回一个值。你的编译器应该警告你这个不幸的遗漏。

}
I am able to initialize the pointer to the array as
int (*a)[10];
int b[100]; a = &b[0];
But i am getting the warning
warning: assignment from incompatible pointer type
Of course you are. a is a pointer to an array of 10 ints (which
you probably do not need), while &b[0] is a pointer to an int. Your
compiler is doing you a favor.
My Code is
int main()
int main( void ) /* better */
{
int (*a)[10];
int b[100];
int i; for(i=0;i<100;i++)
for( i=0; i < sizeof b; i++) /* better */
b[i]=i; a = &b[0];
Wrong, as I said.
printf(" %u %u %d %d\n",a ,&b[0],**a,b[0]);
Wrong in multiple ways:

1) You did not include <stdio.h>.
2) %u is not the conversion specifier you want for pointers.

printf( " %p %p %d %d\n", (void*)a, (void*)&b[0], **a, b[0] );

Note the casts; they are required.
a++;
printf(" %u %u %d %d\n",a ,&b[10],**a,b[10]);
a++;
printf(" %u %u %d %d\n",a , &b[20],**a,b[20]);
a++;
printf(" %u %u %d %d\n",a , &b[30],**a,b[30]);
Unless you are using a C99 compiler, you must return a value from
main(). Your compiler should have warned you about this unfortunate
omission.
}




-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


Christopher Benson-Manica写道:


< snipped rest of code>
Christopher Benson-Manica wrote:

<snipped rest of code>
ru********@rediffmail.com 写道:
int main()
int main()


int main(void)/ *更好* /



int main( void ) /* better */

{
int(* a)[10];
int b [100];
int i;
{
int (*a)[10];
int b[100];
int i;


for(i = 0; i< 100; i ++ )
for(i=0;i<100;i++)



for(i = 0; i< sizeof b; i ++)/ * better * /



for( i=0; i < sizeof b; i++) /* better */




Shouldn'那是:

for(i = 0; i<((sizeof b)/(sizeof int)); i ++)

??



Shouldn''t that be:
for (i = 0; i < ((sizeof b) / (sizeof int)); i++)
??


这篇关于将指针初始化为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆