Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中更新数组中的值_C_Arrays_Segmentation Fault - Fatal编程技术网

在C中更新数组中的值

在C中更新数组中的值,c,arrays,segmentation-fault,C,Arrays,Segmentation Fault,我有一个数组,其值初始化为整数值。当我尝试更改值并打印到文件时,代码会编译,但在执行时返回“分段错误”错误。任何想法都将不胜感激 int theArray[50]; ...//code setting the array values to zero int i; for(i = 0; i < 50; i++) { ...//code setting int "p" to some number between -100 and 100 if (p < 25 || p >

我有一个数组,其值初始化为整数值。当我尝试更改值并打印到文件时,代码会编译,但在执行时返回“分段错误”错误。任何想法都将不胜感激

int theArray[50];
...//code setting the array values to zero
int i;
for(i = 0; i < 50; i++)
{
  ...//code setting int "p" to some number between -100 and 100
  if (p < 25 || p > -25)
  {
  int temp = p + 25;
  int currentVal = theArray[temp];
  theArray[temp] = currentVal + 1;
  }
}
intthearray[50];
…//将数组值设置为零的代码
int i;
对于(i=0;i<50;i++)
{
…//将int“p”设置为-100和100之间的某个数字的代码
如果(p<25 | | p>-25)
{
内部温度=p+25;
int currentVal=阵列[温度];
阵列[温度]=当前值+1;
}
}

当我执行更改“currentVal”的步骤时,没有分段故障。提前谢谢

您从未声明数组的大小,因此它是一个长度为0的数组(没有分配整数)。因此,当您尝试初始化数组的一部分时,您会由于访问不允许访问的内存而出现分段错误

要解决此问题,请为数组指定一个编译时常量数组大小:

int theArray[50];
...//code setting the array values to zero
int i;
for(i = 0; i < 50; i++)
{
  int currentVal = theArray[i]; // make sure you've initialized the array before doing this
  theArray[i] = currentVal + 1;
}
intthearray[50];
…//将数组值设置为零的代码
int i;
对于(i=0;i<50;i++)
{
int currentVal=theArray[i];//执行此操作之前,请确保已初始化数组
阵列[i]=currentVal+1;
}

您从未声明数组的大小,因此它是一个长度为0的数组(未分配任何整数)。因此,当您尝试初始化数组的一部分时,您会由于访问不允许访问的内存而出现分段错误

要解决此问题,请为数组指定一个编译时常量数组大小:

int theArray[50];
...//code setting the array values to zero
int i;
for(i = 0; i < 50; i++)
{
  int currentVal = theArray[i]; // make sure you've initialized the array before doing this
  theArray[i] = currentVal + 1;
}
intthearray[50];
…//将数组值设置为零的代码
int i;
对于(i=0;i<50;i++)
{
int currentVal=theArray[i];//执行此操作之前,请确保已初始化数组
阵列[i]=currentVal+1;
}

这不是你想要做的吗

int i;
for(i = 0; i < 50; i++) {
    theArray[i] += 1;
}
inti;
对于(i=0;i<50;i++){
阵列[i]+=1;
}
这对你不管用吗

编辑

if(p<25 | | p>-25)
由于对
|
进行短路评估,对于-50、-26等值,将评估为真,这意味着将首先评估
p<25
,如果发现
为真
,则不会评估其他条件

对于像50这样的数字,第二个条件将计算为
true
,temp将再次超出
theArray

在这两种情况下,您都会看到分段错误


如果(p<25&&p>-25)
应该对你有帮助。

这不是你想要做的吗

int i;
for(i = 0; i < 50; i++) {
    theArray[i] += 1;
}
inti;
对于(i=0;i<50;i++){
阵列[i]+=1;
}
这对你不管用吗

编辑

if(p<25 | | p>-25)
由于对
|
进行短路评估,对于-50、-26等值,将评估为真,这意味着将首先评估
p<25
,如果发现
为真
,则不会评估其他条件

对于像50这样的数字,第二个条件将计算为
true
,temp将再次超出
theArray

在这两种情况下,您都会看到分段错误


if(p<25&&p>-25)
应该对您有所帮助。

在输入时,您输入的整数必须大于数组[]的大小,即50。这就是编译器显示分段错误的原因。

在输入时,您输入的整数必须大于数组[]的大小,即,50.这就是为什么编译器显示分段错误。

我几乎可以肯定,如果(p-25)。。。您可能混淆了逻辑运算符。

我几乎可以肯定,如果(p-25)。。。您可能混淆了逻辑运算符。

您的条件是错误的

if (p < 25 || p > -25)
if(p<25 | | p>-25)
如果p为1000,它将输入该if,以及当p为-1000时。您需要AND逻辑运算符

if (p < 25 && p > -25)
if(p<25&&p>-25)
此外,由于有效索引的范围为0到49(含0到49),我认为其中一个运算符必须包含等式,即:

if (p < 25 && p >= -25)
if(p<25&&p>=-25)

您的情况不正确

if (p < 25 || p > -25)
if(p<25 | | p>-25)
如果p为1000,它将输入该if,以及当p为-1000时。您需要AND逻辑运算符

if (p < 25 && p > -25)
if(p<25&&p>-25)
此外,由于有效索引的范围为0到49(含0到49),我认为其中一个运算符必须包含等式,即:

if (p < 25 && p >= -25)
if(p<25&&p>=-25)

您的代码现在将数组的所有值设置为1?是的,按照这里的编写方式。实际版本实际上在几个方面有所不同。我只是想根据我已经测试和“消除”的内容来隔离问题。如果我将其更改为与原始版本更相似,是否会有所帮助?请检查您在问题中显示的代码是否实际显示了您描述的问题。。。您现在显示的内容应该没有问题,因此可能缺少或不同的内容导致了您的问题。这会很有帮助,因为您不知道是什么导致了错误。可能错误在其他地方对于有效的数组索引,
p+25
必须在0到49之间,因此
p
需要至少为-25,最多为24。因此,尝试一下,
如果(p<25&&p>=-25)
您的代码现在将数组的所有值都设置为1?按照这里编写的方式,是的。实际版本实际上在几个方面有所不同。我只是想根据我已经测试和“消除”的内容来隔离问题。如果我将其更改为与原始版本更相似,是否会有所帮助?请检查您在问题中显示的代码是否实际显示了您描述的问题。。。您现在显示的内容应该没有问题,因此可能缺少或不同的内容导致了您的问题。这会很有帮助,因为您不知道是什么导致了错误。可能错误在中的有效数组的其他地方