如何获取一个数组";“字符串”;并将它们放入C中的字符数组中?

如何获取一个数组";“字符串”;并将它们放入C中的字符数组中?,c,string,function,integer,character,C,String,Function,Integer,Character,我有一个表示名称的结构中的字符数组。我可以在我的主函数中很好地打印它们,但是如果我将它们传递给另一个函数,它们将被读取为整数。如何将相同的值传递到函数中并打印出名称 例如,我目前有 #include <stdio.h> #include <stdlib.h> #include <string.h> #include "student_struct.c" struct Student{ char name[50]; int id; fl

我有一个表示名称的结构中的字符数组。我可以在我的主函数中很好地打印它们,但是如果我将它们传递给另一个函数,它们将被读取为整数。如何将相同的值传递到函数中并打印出名称

例如,我目前有

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "student_struct.c"

struct Student{
    char name[50];
    int id;
    float gpa;
    int age;
};

int main(){

    FILE *fptr;
    fptr = fopen("student_records.txt", "r");

    struct Student students[100] = {0};

    //Declaring fields
    int i;
    unsigned int counter = 0;

    //Take data from the file
    for( int i = 0; i < 100; i++) {
        if(fscanf(fptr, "%49s %d %f %d",
            students[i].name,
            &students[i].id,
            &students[i].gpa,
            &students[i].age
        ) != 4) {
        break;
    }
        counter++;
    }

    //Creating a new names array to pass through the function
    char names[counter];
    for(i = 0; i < counter; i++){
        names[i] = *students[i].name; 
    //I see that I'm using the asterisk, which I believe is what's giving me 
    //the integer value, but when I delete it, I get an an error
    //that says "assignment makes integer from pointer without a cast"
    }


    //To close the file
    fclose(fptr);

    //Calling function
    calcHighGPA(names, gpas, counter);

    return 0;
}
调用函数时,
printf
行需要一个整数,但我需要一个“字符串”

如果需要参考,我正在读取的文本文件:

David 1234 4.0 44
Sally 4321 3.6 21
Bob 1111 2.5 20
Greg 9999 1.8 28
Heather 0000 3.2 22
Keith 3434 2.7 40
Pat 1122 1.0 31
Ann 6565 3.0 15
Mike 9898 2.0 29
Steve 1010 2.2 24
Kristie 2222 3.9 46

谢谢您的帮助。

您正在calcHighGPA()中传递一个字符串(字符数组名称),而我猜您需要一个字符串数组! 在下面的代码片段中

char names[counter];
for(i = 0; i < counter; i++){
    names[i] = *students[i].name; 
}
char名称[计数器];
对于(i=0;i
实际上你并不是在复制所有的名字。而是只复制每个字符串(字符串)的第一个字符


您在哪里声明和定义传递给calcHighGPA的GPA数组?

names[i]=*students[i].name
使
名称
包含每个
学生[i].name
字符串的第一个字符<代码>名称
也不是以nul结尾的,不能用作字符串。它将是一个简单的字符数组
name[nameGPA]
是单个字符(它调用未定义的行为,因为
“%s”
name[nameGPA]
..不匹配)。哎呀,为了清晰和简洁起见,我从帖子中删除了它。对于如何将每个名称复制到数组中,您有什么建议吗?我不确定我的问题是什么,所以我不知道该用谷歌搜索什么。谢谢你的帮助@帮助我您可以使用或将所有名称复制到您的名称数组中,请浏览手册页以正确使用它们
char names[counter];
for(i = 0; i < counter; i++){
    names[i] = *students[i].name; 
}