C 与指针相关的警告
我必须创建一个函数,为一个新数组分配动态内存,然后用[low;high]间隔中的随机数填充该数组。如果函数成功,则返回指向新数组第一位的指针,如果失败,则返回NULL。我的程序运行得非常好,我得到了我需要的结果,但我得到了两个警告。1) 警告:从返回类型为“int”的函数中返回“void*”会使。。。2) 警告:正在从不兼容的指针类型传递“createArray”的参数1。哦,在这个C 与指针相关的警告,c,C,我必须创建一个函数,为一个新数组分配动态内存,然后用[low;high]间隔中的随机数填充该数组。如果函数成功,则返回指向新数组第一位的指针,如果失败,则返回NULL。我的程序运行得非常好,我得到了我需要的结果,但我得到了两个警告。1) 警告:从返回类型为“int”的函数中返回“void*”会使。。。2) 警告:正在从不兼容的指针类型传递“createArray”的参数1。哦,在这个else if(firstDigit==NULL)中,我得到了“警告:指针和整数之间的比较”。我尝试将我的函数改为
else if(firstDigit==NULL)
中,我得到了“警告:指针和整数之间的比较”。我尝试将我的函数改为int*createArray(int*pt,int size)
而不是简单的int-createArray(int*pt,int size)
,但这没有帮助
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int createArray(int *pt, int size) {
srand(time(NULL));
int *x;
int count = 0;
int low = 2;
int high = 5;
x = (int*) malloc(size * sizeof(int));
if(x == NULL) {
printf("Memory cannot be allocated\n");
exit(0);
}
//filling the array with random numbers from [low;high]
for(int i = 0;i < size; ++i) {
x[i] = low + rand()%(high-low+1);
++count;
}
//returning a pointer to the first digit of the new array
if(count == size) {
pt = &x[0];
return *pt;
}
else {
return NULL;
}
free(x);
}
int main()
{
int *pt;
int size = 5;
int firstDigit = createArray(&pt, size);
if(firstDigit > 0){
printf("First digit is: %d", firstDigit);
}
else if(firstDigit == NULL) {
printf("Something went wrong");
}
}
#包括
#包括
#包括
int createArray(int*pt,int size){
srand(时间(空));
int*x;
整数计数=0;
int低=2;
int高=5;
x=(int*)malloc(size*sizeof(int));
如果(x==NULL){
printf(“无法分配内存\n”);
出口(0);
}
//用来自[低;高]的随机数填充数组
对于(int i=0;i0){
printf(“第一个数字是:%d”,第一个数字);
}
else if(firstDigit==NULL){
printf(“出了问题”);
}
}
有各种各样的问题,有些严重,有些平凡
createArray()
底部的free(x)
是无法访问的代码;前面的if
和else
子句都以return结尾。您正在泄漏在createArray()
中分配的内存-您没有成功地将指向内存的指针返回到main()
函数(并且您也没有尝试在那里释放它),并且您没有成功地在函数中释放它-如果它也破坏了它创建的数组,那么这就不太像“创建数组”函数了
对需求的一种解释(称之为“解释A”)是函数应该返回int*
,而不是int
。您可能会收到关于将指针转换为整数(可能提到“不同大小”)的返回NULL的警告
在这种解释下,main()
中的代码可能应该使用int*firstDigit=createArray(…)代码>。然后在else if(firstDigit==NULL){
中进行比较是正确的,但是前面的if(firstDigit>0)
是可疑的,您实际上应该反转测试:
if (firstDigit == NULL)
{
fprintf(stderr, "Something went wrong with memory allocation\n");
exit(1);
}
printf("First digit is: %d\n", firstDigit[0]);
请注意:
- 错误转到stderr
- 消息以换行符结尾
- 程序在失败时应以非零状态退出
在这种解释下,您不会将pt
或&pt
传递给函数;您只需将函数的返回值分配给pt
但是,另一种解释(称之为“解释B”)是,您试图将分配内存的指针分配给main()
中的pt
,但是createArray()
的参数中的指针类型不匹配。这对您的原因也没有帮助
固定解释代码A:
解释B的固定代码:
我会注意到解释B不太灵活,不像惯用的C代码。这没有错,但它不是通常的做法。当你拥有整个数组时,第一个数字很容易获得。我们还可以讨论从函数中退出的智慧——最好返回空指针并使用CALIng代码报告错误。对于库中的代码尤其如此。这也会影响函数的设计。存在各种各样的问题,有些严重,有些平凡
createArray()
底部的free(x)
是无法访问的代码;它前面的if
和else
子句都以return结尾。您正在泄漏在createArray()
中分配的内存-您没有成功地将指向内存的指针返回到main()
函数(并且您也没有尝试在那里释放它),并且您没有在函数中成功地释放它-如果“创建数组”函数也破坏了它创建的数组,那么它就不会像“创建数组”函数那样了
对这些要求的一种解释(称之为“解释A”)是,您的函数应该返回int*
,而不是int
。您可能会收到关于return NULL
将指针转换为整数的警告(可能提到“不同大小”)
在这种解释下,main()
中的代码可能应该使用int*firstDigit=createArray(…);
。那么else中的比较if(firstDigit==NULL){
是正确的,但是前面的if(firstDigit>0)
是可疑的,实际上应该反向测试:
if (firstDigit == NULL)
{
fprintf(stderr, "Something went wrong with memory allocation\n");
exit(1);
}
printf("First digit is: %d\n", firstDigit[0]);
请注意:
- 错误转到stderr
- 消息以换行符结尾
- 程序在失败时应以非零状态退出
在这种解释下,您不会将pt
或&pt
传递给函数;您只需将函数的返回值分配给pt
但是,另一种解释(称之为“解释B”)是,您试图将分配内存的指针分配给main()中的pt
First digit is: 5
[0] 5
[1] 8
[2] 6
[3] 6
[4] 2
/* SO 6463-4370 */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static int createArray(int **pt, int size)
{
int *x;
int count = 0;
int low = 2;
int high = 5;
x = (int *) malloc(size * sizeof(int));
if (x == NULL)
{
fprintf(stderr, "Memory cannot be allocated\n");
exit(1);
}
// filling the array with random numbers from [low;high]
for (int i = 0; i < size; ++i)
{
x[i] = low + rand() % (high - low + 1);
++count;
}
// Assigning a pointer to the first digit of the new array,
// and returning the first digit of the array
*pt = x;
return x[0];
}
int main(void)
{
srand(time(NULL));
int *pt;
int size = 5;
int firstDigit = createArray(&pt, size);
if (firstDigit == 0)
{
fprintf(stderr, "Something went wrong\n");
exit(1);
}
printf("First digit is: %d\n", firstDigit);
for (int i = 0; i < size; i++)
printf(" [%d] %d\n", i, pt[i]);
free(pt);
return 0;
}
First digit is: 3
[0] 3
[1] 3
[2] 2
[3] 4
[4] 4
int* createArray(int size) {
srand(time(NULL));
int *x;
/* no need to verify loop runs correct # of times
int count = 0;
*/
int low = 2;
int high = 5;
x = /*do not cast malloc calls */ malloc(size * sizeof(int));
if(x == NULL) {
printf("Memory cannot be allocated\n");
// exit with nonzero value to indicate error
exit(1);
}
//filling the array with random numbers from [low;high]
for(int i = 0;i < size; ++i) {
x[i] = low + rand()%(high-low+1);
++count;
}
//returning a pointer to the first digit of the new array
/* no need to verify that the loop has run the correct # of times
if(count == size) { */
return x;
/* no need for this check
else {
return NULL;
}
free(x);*/
}
int main()
{
int *pt;
int size = 5;
// adapting to new signature
pt = createArray(size);
// check for NULL before trying to use value
// even though you decide to exit(1) on failure, so pt will never be NULL
if (pt == NULL) {
printf("Something went wrong\n");
return 1;
}
// accessing first element
else if (pt[0] > 0) {
printf("First digit is %d\n", pt[0]);
}
free(pt);
}