新的程序员问题 [英] new c programmer question

查看:75
本文介绍了新的程序员问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的名字是John,这是我第一次发帖到comp.lang.c.我是b $ b学习C.我是一名对编程有兴趣的高中生。我b $ b有一份C编程语言第二版Brian W

Kernighan和Dennis M Ritchie。我在SuSE 9上使用gcc 4.1版。


这是我的简单问题。我想设置一个矩阵,在我的程序中包含一些

值。我想通过

中的地址传递矩阵数组函数。编译器说:


hello.c:15:警告:从不兼容的指针类型分配


我可以看到有什么问题但是做的没有能力修复它。这里

是错误所在的小代码。我会很感激有关

语法的建议。


谢谢

John


void setupmatrix(int * m [])

{

int x [4] [4] =

{

{1,2,3,4},

{5,6,7,8},

{9,10,11,12 },

{13,14,15,16}

};

* m = x;

}

解决方案

John Gerrard schrieb:


我的名字是约翰,这是我第一次发布到comp.lang.c.我是b $ b学习C.我是一名对编程有兴趣的高中生。我b $ b有一份C编程语言第二版Brian W

Kernighan和Dennis M Ritchie。我在SuSE 9上使用gcc 4.1版。



感谢您提供这样的背景 - 这不是严格的

当然需要,但帮助我们解释和

建议:-)

你有一本好书,但不要错过勘误表:
http://cm.bell-labs.com/cm/ cs / cbook / 2ediffs.html

有些东西相当微妙,但其他东西不是;例如,

使用

int main(无效)

而不是

main()

,如您的印刷版本所示。


这是我的简单问题。我想设置一个矩阵,在我的程序中包含一些

值。我想通过

中的地址传递矩阵数组函数。编译器说:


hello.c:15:警告:从不兼容的指针类型分配


我可以看到有什么问题但是做的没有能力修复它。这里

是错误所在的小代码。我会很感激有关

语法的建议。



请解释_you_认为错误的原因 - 有可能是你错了并试图治愈错误的病。

如果您提供完整的,编译的

(但警告/错误)程序,出于同样的原因会更好。


void setupmatrix(int * m [])



这与

int ** m

使用后者而不是前者,因为它是不必要的

模糊。


{

int x [4] [4] =

{

{1,2,3,4},

{5, 6,7,8},

{9,10,11,12},

{13,14,15,16}

};



x是一个包含4个元素类型数组的数组,带有

4个int类型的元素,具有自动存储持续时间,即

它只能从这里生活到右大括号。


* m = x;



* m是指向int的指针。

& x [0] [0]也是指向int的指针。 />
但是,即使你在x [0] [0]的地址上做了* m点,

你从
返回后也无法使用这个地址
函数 - x不再存在,所以试图访问它

post mortem导致未定义的行为,这可能是关于

你程序崩溃的任何事情,似乎工作,似乎

有时让恶魔飞出你的鼻子。


问题是:你的目标是什么?

也就是说,你想要复制x的内容吗?

然后你不需要传递一个int **但是起始地址

要么是一个int数组足够大以占用16个整数或者包含在x中的
或具有4个元素类型的数组的数组

int足够大以至少取4个行数x。

这使得

void setupmatrix(int * m)



void setupmatrix(int( * m)[4])


或者你想设置一些指向x开头的指针?在这个

的情况下,你必须通过给它静态存储持续时间来确保x生活在
setupmatrix()之外:

static int x [4] [4]

在前两种情况下,你需要将

x的内容复制到m个点。

int i,j,k;

for(i = 0,j = 0; j< 4; ++ j)

for(k = 0; k< 4; ++ k)

m [i ++] = x [j] [k];

或者,在第二种情况下

for(j = 0; j <4; ++ j)

for(k = 0; k <4; ++ k)

m [j] [ k] = x [j] [k];

(更简单,因为m与x具有相同的布局)

甚至使用

memcpy(& m [0],& x [0] [0],sizeof x);



memcpy(& m [0] [0 ],& x [0] [0],sizeof x);


如果m具有不同的布局,则循环更容易更改

比x例如,如果m指向一个具有不同数量的bb的对象,那么


}


还可以查看comp.lang.c常见问题解答
http://c-faq.com

特别是关于数组和指针的章节。


干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。

感谢您花时间写下这一切迈克尔。你的笔记

只是强化了我不知道多少。我需要完全消化和理解你所写的内容(而不是采用简单的选项和

只是发布了越来越多的问题)。我希望在未来几个月内提出更高级的

问题!


问候

John

Michael Mair写道:


John Gerrard schrieb:


我叫约翰,这是我的首先发布到comp.lang.c.我是b $ b学习C.我是一名对编程有兴趣的高中生。我b $ b有一份C编程语言第二版Brian W

Kernighan和Dennis M Ritchie。我在SuSE 9上使用gcc 4.1版。



感谢您提供此背景 - 这不是严格的

当然,但帮助我们解释和

建议:-)

你有一本好书,但不要错过勘误表:
http://cm.bell-labs.com/cm/ cs / cbook / 2ediffs.html

有些东西相当微妙,但其他东西不是;例如,

使用

int main(无效)

而不是

main()

,如您的印刷版本所示。


这是我的简单问题。我想设置一个矩阵,在我的程序中包含一些

值。我想通过

中的地址传递矩阵数组函数。编译器说:


hello.c:15:警告:从不兼容的指针类型分配


我可以看到有什么问题但是做的没有能力修复它。这里

是错误所在的小代码。我会很感激有关

语法的建议。



请解释_you_认为错误的原因 - 有可能是你错了并试图治愈错误的病。

如果您提供完整的,编译的

(但警告/错误)程序,出于同样的原因会更好。


void setupmatrix(int * m [])



这与

int ** m

使用后者而不是前者,因为它是不必要的

模糊。


{

int x [4] [4] =

{

{1,2,3,4},

{5, 6,7,8},

{9,10,11,12},

{13,14,15,16}

};



x是一个包含4个元素类型数组的数组,带有

4个int类型的元素,具有自动存储持续时间,即

它只能从这里生活到右大括号。


* m = x;



* m是指向int的指针。

& x [0] [0]也是指向int的指针。 />
但是,即使你在x [0] [0]的地址上做了* m点,

你从
返回后也无法使用这个地址
函数 - x不再存在,所以试图访问它

post mortem导致未定义的行为,这可能是关于

你程序崩溃的任何事情,似乎工作,似乎

有时让恶魔飞出你的鼻子。


问题是:你的目标是什么?

也就是说,你想要复制x的内容吗?

然后你不需要传递一个int **但是起始地址

要么是一个int数组足够大以占用16个整数或者包含在x中的
或具有4个元素类型的数组的数组

int足够大以至少取4个行数x。

这使得

void setupmatrix(int * m)



void setupmatrix(int( * m)[4])


或者你想设置一些指向x开头的指针?在这个

的情况下,你必须通过给它静态存储持续时间来确保x生活在
setupmatrix()之外:

static int x [4] [4]

在前两种情况下,你需要将

x的内容复制到m个点。

int i,j,k;

for(i = 0,j = 0; j< 4; ++ j)

for(k = 0; k< 4; ++ k)

m [i ++] = x [j] [k];

或者,在第二种情况下

for(j = 0; j <4; ++ j)

for(k = 0; k <4; ++ k)

m [j] [ k] = x [j] [k];

(更简单,因为m与x具有相同的布局)

甚至使用

memcpy(& m [0],& x [0] [0],sizeof x);



memcpy(& m [0] [0 ],& x [0] [0],sizeof x);


如果m具有不同的布局,则循环更容易更改

比x例如,如果m指向具有不同

数量的列的对象。


}



还可以看看
上的comp.lang.c常见问题解答 http://c-faq.com

特别是有关数组和指针的章节。


干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。


John Gerrard写道:


您好,我的名字是John,这是我的第一个发布到comp.lang.c.



欢迎来到小组。


我是

学习C.我是一名对编程感兴趣的高中生。我b $ b有一份C编程语言第二版Brian W

Kernighan和Dennis M Ritchie。我在SuSE 9上使用gcc 4.1版。



编译器和操作系统与你的问题无关,这很好

因为我们这里不讨论编译器/操作系统特定的问题。如果你不知道是否某些东西是编译器/操作系统特定的,那么我们将很乐意告诉你,并试着指出你正确的方向。


这是我的简单问题。我想设置一个矩阵,在我的程序中包含一些

值。我想通过

中的地址传递矩阵数组函数。编译器说:


hello.c:15:警告:从不兼容的指针类型赋值



你是对的关注那个警告,因为它显示了一个严重的问题。你有其他编译器目前没有警告的人。


< OT>

我建议编译

gcc -ansi -pedantic -Wall -O

< / OT>


我可以看到有什么问题,但还没有能力修复它。这里

是错误所在的小代码。我会很感激有关

语法的建议。


谢谢

John


void setupmatrix(int * m [])

{

int x [4] [4] =

{

{1,2,3,4},

{5,6,7,8},

{9,10,11,12 },

{13,14,15,16}

};

* m = x;

}



好​​了,现在就你的问题了。


首先x是一个局部变量,一旦不存在就会立即存在

函数返回以及你的代码尝试做的是在函数外部使用指向

的指针。


其次,参数列表中的[]表示指针不是数组。即

void foo(int i [])

表示*与*完全*相同

void food(int * i)


所以,在你的情况下:

void setupmatrix(int * m [])

实际上意味着:

void setupmatrix(int ** m)

现在,如果你仔细考虑一下,你会发现一个2 $ / $
维数组与指向a的指针根本不同指针。

如果你不相信我出去得到太多指针(棍棒会做)和

指向一个东西,然后指向一个写上

数字5的纸,然后将另一个指针指向第一个。

然后,在第二张纸上画出一个网格(2维)数组)

并在其中加入一些数字。然后比较两者。你会看到

指针指针与网格(二维数组)非常不同。


然后我们就有了猜测的方式你想使用这个功能。我在猜你好b / b
你有类似的东西:


int main(无效)

int arr [4] [4 ];

setupmatrix(arr);

返回0;

}


它会有如果你提供了样品用法,那就很有用了,那我就不用猜测了。不需要猜测。


如果我是正确的那么你还需要明白你不能将
分配给一个数组,只分配给一个数组元素。


我建议你去 http://c-faq.com/

并从阅读开始第6节。问题6.2,6.3,6.4,6.4b,6.5,

6.6 ......实际上,第6节中的大部分与我认为你的事情相关

错过了 - 理解!


还有问题7.5a和7.9(因为我觉得你有误解

)。


一旦你阅读了这些,就再试一次并发布你的尝试,

in包括一个调用它的示例主函数,我们将分析它

并告诉你任何问题。

-

Flash Gordon


Hi, my name is John and this is my first posting to comp.lang.c. I am
learning C. I am a high school student with interest in programming. I
have a copy of "The C programming language second edition by Brian W
Kernighan and Dennis M Ritchie". I am using gcc version 4.1 on SuSE 9.

Here is my simple question. I want to set a matrix to contain some
values in my program. I want to pass in the matrix array by address in
a function. The compiler is saying:

hello.c:15: warning: assignment from incompatible pointer type

I can see what is wrong but do not have the ability yet to fix it. Here
is the small code where the error is. I would appreciate some advice on
the syntax to use.

Thank you
John

void setupmatrix(int *m[])
{
int x[4][4]=
{
{ 1, 2, 3, 4},
{ 5, 6, 7, 8},
{ 9,10,11,12},
{13,14,15,16}
};
*m=x;
}

解决方案

John Gerrard schrieb:

Hi, my name is John and this is my first posting to comp.lang.c. I am
learning C. I am a high school student with interest in programming. I
have a copy of "The C programming language second edition by Brian W
Kernighan and Dennis M Ritchie". I am using gcc version 4.1 on SuSE 9.

Thank you for provding this background -- this is not strictly
necessary, of course, but helps us with the explanations and
suggestions :-)
You have a good book there, but don''t miss the errata:
http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html
Some stuff there is rather subtle but other is not; for example,
use
int main (void)
rather than
main ()
as suggested in your printed version.

Here is my simple question. I want to set a matrix to contain some
values in my program. I want to pass in the matrix array by address in
a function. The compiler is saying:

hello.c:15: warning: assignment from incompatible pointer type

I can see what is wrong but do not have the ability yet to fix it. Here
is the small code where the error is. I would appreciate some advice on
the syntax to use.

Please explain what _you_ think is wrong -- it is possible that
you are mistaken and tried to cure the wrong ill.
It would have been better if you provided a complete, compiling
(but for the warning/error) programme, for the same reason.

void setupmatrix(int *m[])

This is the same as
int **m
Use the latter rather than the former because it is unnecessarily
obscure.

{
int x[4][4]=
{
{ 1, 2, 3, 4},
{ 5, 6, 7, 8},
{ 9,10,11,12},
{13,14,15,16}
};

x is an array with 4 elements of type array with
4 elements of type int with automatic storage duration, i.e.
it lives only from here to the closing brace.

*m=x;

*m is a pointer to int.
&x[0][0] also is a pointer to int.
However, even if you made *m point at the address of x[0][0],
you could not use this address after you returned from the
function -- x does no longer live, so trying to access it
post mortem leads to undefined behaviour, which may be about
anything from your programme crashing, seeming to work, seeming
to work sometimes to having demons flying out your nose.

The question is: What was your aim?
That is, did you want to copy the contents of x?
Then you need not pass an int** but the start address of
either an int array sufficiently large to take the 16 ints
contained in x or an array of arrays with 4 elements of type
int sufficiently large to take at least the 4 "rows" of x.
This makes
void setupmatrix(int *m)
or
void setupmatrix(int (*m)[4])

Or do you want to set some pointer to the start of x? In this
case, you have to make sure that x lives even outside of
setupmatrix() by giving it static storage duration:
static int x[4][4]

In the former two cases, you need to copy the contents of
x to whatever m points to.
int i, j, k;
for (i=0, j=0; j<4; ++j)
for (k=0; k<4; ++k)
m[i++] = x[j][k];
or, in the second case
for (j=0; j<4; ++j)
for (k=0; k<4; ++k)
m[j][k] = x[j][k];
(easier, because m has the same layout as x)
or even using
memcpy(&m[0], &x[0][0], sizeof x);
or
memcpy(&m[0][0], &x[0][0], sizeof x);

The loops are easier to change if m has a different layout
than x, for example if m points to an object with different
number of "columns".

}

Have also a look at the comp.lang.c FAQ at
http://c-faq.com
especially the chapter about arrays and pointers.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


Thanks for taking the time about to write all this Michael. Your notes
just reinforce how much I don''t know. I need to digest and understand
fully what you have written (rather than taking the easy option and
just posting more and more questions). I hope to ask more advanced
questions in the months ahead !

Regards
John
Michael Mair wrote:

John Gerrard schrieb:

Hi, my name is John and this is my first posting to comp.lang.c. I am
learning C. I am a high school student with interest in programming. I
have a copy of "The C programming language second edition by Brian W
Kernighan and Dennis M Ritchie". I am using gcc version 4.1 on SuSE 9.


Thank you for provding this background -- this is not strictly
necessary, of course, but helps us with the explanations and
suggestions :-)
You have a good book there, but don''t miss the errata:
http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html
Some stuff there is rather subtle but other is not; for example,
use
int main (void)
rather than
main ()
as suggested in your printed version.

Here is my simple question. I want to set a matrix to contain some
values in my program. I want to pass in the matrix array by address in
a function. The compiler is saying:

hello.c:15: warning: assignment from incompatible pointer type

I can see what is wrong but do not have the ability yet to fix it. Here
is the small code where the error is. I would appreciate some advice on
the syntax to use.


Please explain what _you_ think is wrong -- it is possible that
you are mistaken and tried to cure the wrong ill.
It would have been better if you provided a complete, compiling
(but for the warning/error) programme, for the same reason.

void setupmatrix(int *m[])


This is the same as
int **m
Use the latter rather than the former because it is unnecessarily
obscure.

{
int x[4][4]=
{
{ 1, 2, 3, 4},
{ 5, 6, 7, 8},
{ 9,10,11,12},
{13,14,15,16}
};


x is an array with 4 elements of type array with
4 elements of type int with automatic storage duration, i.e.
it lives only from here to the closing brace.

*m=x;


*m is a pointer to int.
&x[0][0] also is a pointer to int.
However, even if you made *m point at the address of x[0][0],
you could not use this address after you returned from the
function -- x does no longer live, so trying to access it
post mortem leads to undefined behaviour, which may be about
anything from your programme crashing, seeming to work, seeming
to work sometimes to having demons flying out your nose.

The question is: What was your aim?
That is, did you want to copy the contents of x?
Then you need not pass an int** but the start address of
either an int array sufficiently large to take the 16 ints
contained in x or an array of arrays with 4 elements of type
int sufficiently large to take at least the 4 "rows" of x.
This makes
void setupmatrix(int *m)
or
void setupmatrix(int (*m)[4])

Or do you want to set some pointer to the start of x? In this
case, you have to make sure that x lives even outside of
setupmatrix() by giving it static storage duration:
static int x[4][4]

In the former two cases, you need to copy the contents of
x to whatever m points to.
int i, j, k;
for (i=0, j=0; j<4; ++j)
for (k=0; k<4; ++k)
m[i++] = x[j][k];
or, in the second case
for (j=0; j<4; ++j)
for (k=0; k<4; ++k)
m[j][k] = x[j][k];
(easier, because m has the same layout as x)
or even using
memcpy(&m[0], &x[0][0], sizeof x);
or
memcpy(&m[0][0], &x[0][0], sizeof x);

The loops are easier to change if m has a different layout
than x, for example if m points to an object with different
number of "columns".

}


Have also a look at the comp.lang.c FAQ at
http://c-faq.com
especially the chapter about arrays and pointers.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


John Gerrard wrote:

Hi, my name is John and this is my first posting to comp.lang.c.

Welcome to the group.

I am
learning C. I am a high school student with interest in programming. I
have a copy of "The C programming language second edition by Brian W
Kernighan and Dennis M Ritchie". I am using gcc version 4.1 on SuSE 9.

The compiler and OS are not relevant to your problem, which is good
since we don''t deal with compiler/OS specific issues here. If you don''t
know whether something is compiler/OS specific then we will be happy to
tell you and try to point you in the correct direction.

Here is my simple question. I want to set a matrix to contain some
values in my program. I want to pass in the matrix array by address in
a function. The compiler is saying:

hello.c:15: warning: assignment from incompatible pointer type

You are correct to be concerned by that warning since it shows a serious
problem. You have others the compiler is not currently warning about.

<OT>
I suggest compiling with
gcc -ansi -pedantic -Wall -O
</OT>

I can see what is wrong but do not have the ability yet to fix it. Here
is the small code where the error is. I would appreciate some advice on
the syntax to use.

Thank you
John

void setupmatrix(int *m[])
{
int x[4][4]=
{
{ 1, 2, 3, 4},
{ 5, 6, 7, 8},
{ 9,10,11,12},
{13,14,15,16}
};
*m=x;
}

OK, now for your problems.

Firstly x is a local variable that will cease to exist as soon as the
function returns and what your code attempts to do is make a pointer to
it available outside the function.

Secondly, [] in a parameter list means pointer not array. I.e.
void foo(int i[])
means *exactly* the same as
void food(int *i)

So, in your case:
void setupmatrix(int *m[])
actually means:
void setupmatrix(int **m)
Now, if you think about it carefully, you will realise that a 2
dimensional array is fundamentally different to a pointer to a pointer.
If you don''t believe me go out and get too pointers (sticks will do) and
point one at something, then point one at a piece of paper with the
number 5 written on it, then point the other pointer at the first one.
Then, on a second piece of paper draw out a grid (2 dimensional array)
and put some numbers in it. Then compare the two. You will see that the
pointer to a pointer is very different from the grid (2 dimensional array).

Then we have how I''m guessing you want to use the function. I''m guessing
you have something like:

int main(void)
int arr[4][4];
setupmatrix(arr);
return 0;
}

It would have been useful if you had provided sample usage, then I would
not have to guess.

If I am correct then you also need to understand that you cannot assign
to an array, only to elements of an array.

I suggest you go to the comp.lang.c FAQ available at http://c-faq.com/
and start off by reading section 6. Questions 6.2, 6.3, 6.4, 6.4b, 6.5,
6.6... actually, most of section 6 is relevant to things I think you
have miss-understood!

Also question 7.5a and 7.9 (since I think you have misunderstanding
there as well).

Once you have read these give it another go and post your attempt,
including a sample main function that calls it, and we will analyse it
and tell you about any problems.
--
Flash Gordon


这篇关于新的程序员问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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