【问题标题】:C - Array returns different values from the same element when put into variableC - 数组放入变量时从同一元素返回不同的值
【发布时间】:2020-01-24 23:37:45
【问题描述】:

我对编程很陌生。 简而言之,我的问题:

arr[0]=1 arr[1]=2 arr[2]= -4
printf("\n Element 0 when in array: %d\n", arr[0]); // Prints 1 as it should
arr[0] = a;
printf(" Element 0 assigned to variable a: %d", a); /* Prints 67 ?? (Elements 1 and 2 
are printed as 0 when assigned different variables.) */

我希望我的“a”变量返回与 arr[0] 相同的值(然后对 arr[1] 和 arr[2] 的另外两个变量执行此操作)

我有一个名为“Dane.txt”的带有几个数字的文本文件,它的内容是“1、2、-4”。

这是我尝试使用从 .txt 文件中获取的输入来解决数学问题的完整代码(大部分来自 here):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]){
    
    // Initializing the file pointer
    FILE *fs;
 
    char ch, buffer[32];
    int i = 0, arr[100], j = 0;
    int a, b, c, arr2[3];
    
    // Openning the file with file handler as fs
    fs = fopen("Dane.txt", "r");                    // "Dane.txt" contents: 1, 2, -4,
 
    // Read the file unless the file encounters an EOF
    while(1){
        // Reads the character where the seeker is currently
        ch = fgetc(fs);
 
        // If EOF is encountered then break out of the while loop
        if(ch == EOF){
            break;
        }
        
        // If the delimiter is encounterd(which can be
        // anything according to your wish) then skip the character
        // and store the last read array of characters in
        // an integer array
        else if(ch == ','){
 
            // Converting the content of the buffer into
            // an array position
            arr[j] = atoi(buffer);
 
            // Incrementing the array position
            j++;
 
            // Clearing the buffer, this function takes two
            // arguments, one is a character pointer and 
            // the other one is the size of the character array
            memset(buffer, 0, 32);
 
            // clearing the counter which counts the number
            // of character in each number used for reading
            // into the buffer.
            i = 0;
 
            // then continue
            continue;
        }
        else{
 
            // reads the current character in the buffer
            buffer[i] = ch;
 
            // incrementing the counter so that the next
            // character is read in the next position in 
            // the array of buffer
            i++;
        }
    }
    for(i = 0; i < j; i++){
        arr2[i] = arr[i];
        printf("Number [%d]: %d\n", i, arr[i]);
        printf("%d\n", arr[i]);
    }
    
    printf("\n Element 0 when in array: %d\n", arr[0]); // Returns 1 as it should
    arr[0] = a;
    printf(" Element 0 assigned to variable a: %d", a); // Returns 67 ??
}

【问题讨论】:

  • a = arr[0]而不是arr[0] = a

标签: c arrays file variables


【解决方案1】:

您的代码中没有任何地方可以为a 分配任何值,因此当您打印a 的值时,您会得到该内存区域中发生的任何垃圾。

arr[0] = a;

这是一个任务。左侧是分配给的变量arr[0]。右侧是分配给它的值。如果您对此不清楚,不妨研究一下关于左值和右值的优秀 C 参考资料部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-10
    • 2015-08-15
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 2017-06-21
    • 1970-01-01
    相关资源
    最近更新 更多