C 程序的某些逻辑错误或synatx错误
我正在学习C语言的指针,我试着写一个这样的程序C 程序的某些逻辑错误或synatx错误,c,pointers,C,Pointers,我正在学习C语言的指针,我试着写一个这样的程序 #include <stdio.h> #include <stdlib.h> int* getValue(int length) { int *value,*index,data; value = malloc(length*sizeof(int)); index = value; while(length>0){ scanf("%d", &data); *ind
#include <stdio.h>
#include <stdlib.h>
int* getValue(int length)
{
int *value,*index,data;
value = malloc(length*sizeof(int));
index = value;
while(length>0){
scanf("%d", &data);
*index = data;
index++;
length--;
}
return (value);
}
int total(int length, int *data)
{
int *index,sum,i;
index = data;
sum =0;
for(i=0;i<length;i++){
sum+= *index;
index++;
}
return (sum);
}
int main()
{
int i,j,ptrLength,num,sum,*lengthPtr,*lengthIndex,**ptr,**ptrIndex;
lengthPtr = malloc(3*sizeof(int));
lengthIndex = lengthPtr;
ptr = malloc(3*sizeof(int *));
ptrIndex = ptr;
for(i=0;i<3;i++){
printf("Enter the length and the value:");
scanf("%d",ptrLength);
*lengthIndex = ptrLength;
lengthIndex++;
*ptrIndex = getValue(ptrLength);
ptrIndex++;
}
printf("Enter which one you want to calculate:");
scanf("%d",&num);
sum = total(lengthPtr[num-1], ptr[num-1]);
printf("The sum is %d",sum);
free(lengthPtr);
free(ptr);
return (0);
}
#包括
#包括
int*getValue(int长度)
{
int*值、*索引、数据;
值=malloc(长度*sizeof(int));
指数=价值;
而(长度>0){
scanf(“%d”和数据);
*指数=数据;
索引++;
长度--;
}
回报(价值);
}
整数总计(整数长度,整数*数据)
{
int*索引,总和,i;
指数=数据;
总和=0;
对于(i=0;i唯一的问题是scanf
您需要将其存储在某个地址
scanf("%d",&ptrLength);
我在您的代码中做了一些更改。我使用操作符subscribt[]
迭代数组(int*)(返回对数组中位置n处元素的引用),并修复了一些错误
您必须检查malloc分配是否成功!内存泄漏是因为您没有释放数组(int**)
您刚刚释放了指针
#include <stdio.h>
#include <stdlib.h>
int* getValue(int length)
{
int *value,*index,data;
value = malloc(length*sizeof(int));
if(value==NULL){
printf("malloc failed!");
return NULL;
}
for(int i=0 ;i<length; i++){
scanf("%d", &data);
value[i]=data;
}
return value;
}
int total(int length, int *data)
{
int sum,i;
sum =0;
for(i=0;i<length;i++){
sum+= data[i];
}
return (sum);
}
int main()
{
int i,num,sum;
int *Lengths =(int *)malloc(3*sizeof(int));
if(Lengths==NULL){
printf("malloc failed!");
return 1;
}
int **Arrays = (int **)malloc(3*sizeof(int *));
if(Arrays==NULL){
printf("malloc failed!");
return 1;
}
for(int i=0;i<3;i++){
printf("Enter the Array length: ");
scanf("%d",&Lengths[i]);
printf("Fill the array with values: ");
Arrays[i] = getValue(Lengths[i]);
}
printf("Enter which one you want to calculate:");
scanf("%d",&num);
sum = total(Lengths[num-1], Arrays[num-1]);
printf("The sum is %d",sum);
free(Lengths);
for(int i=0; i<3;i++)
free(Arrays[i]);
free(Arrays);
}
#包括
#包括
int*getValue(int长度)
{
int*值、*索引、数据;
值=malloc(长度*sizeof(int));
如果(值==NULL){
printf(“malloc失败!”);
返回NULL;
}
对于(int i=0;我也是,在scanf(“%d”,ptrLength);
您需要传递变量的地址以接收值:scanf(“%d”,ptrLength);
您正在释放ptr
但不是在getValue()
中分配的每个索引指向的内存。如果您在编译时带有警告(gcc-Wall
)然后您将看到@johnnymapp提醒您的scanf
问题。35:10:警告:未使用的变量“j”[-Wunused variable]
--摆脱它…始终在启用警告的情况下编译。-Wall-Wextra-pedantic-Wshadow
用于gcc/clang或VS/W3
。其他编译器将能够提供警告,您必须查阅文档以获得正确的开关。(指针使用很好)此外,除非检查返回值,否则无法正确使用任何输入函数,例如if(scanf(“%d”,&ptrLength)!=1){/*handle error*/}
。