如何使用函数在struct中填写值?

时间:2018-11-15 13:30:41

标签: c pointers struct

我想调用一个函数来填充C中的一个结构的值。我有以下代码,但出现类似[Error] request for member 'id' in something not a structure or union的错误。

#include <stdio.h> 

typedef struct {
    int       id;
    float    grades[3];
} student_t;

void scan_student (student_t *s) {
    printf("Please give student's info:\n");
    scanf("%d%f%f%f", s.id, s.grades[0], s.grades[1], s.grades[2]);
}

int main () 
{ 
    student_t stu2;

    scan_student(&stu2);

    printf("Student's info are:\n");
    printf("%6d %5.2f %5.2f %5.2f\n", stu2.id, stu2.grades[0], stu2.grades[1], stu2.grades[2]);

    return 0; 
} 

2 个答案:

答案 0 :(得分:9)

Sub CrossSumSolver() Dim StartTime As Double Dim SecondsElapsed As Double StartTime = Timer Dim wb As Workbook Dim ws As Worksheet Set wb = ThisWorkbook Set ws = wb.Worksheets("Sheet1") Application.Calculation = xlCalculationManual Application.ScreenUpdating = False Dim OutputArray(1 To 9) As Long, IsValid As Boolean, CheckLoop As Long Dim a&, b&, c&, d&, e&, f&, g&, h&, i& 'All "As Long" For a = 1 To 9 OutputArray(1) = a For b = 1 To 9 OutputArray(2) = b For c = 1 To 9 OutputArray(3) = c For d = 1 To 9 OutputArray(4) = d For e = 1 To 9 OutputArray(5) = e For f = 1 To 9 OutputArray(6) = f For g = 1 To 9 OutputArray(7) = g For h = 1 To 9 OutputArray(8) = h For i = 1 To 9 OutputArray(9) = i 'Array is populated - is it valid? IsValid = True 'Are all 9 numbers used once? For CheckLoop = 1 To 9 If IsError(Application.Match(CheckLoop, OutputArray, 0)) Then IsValid = False 'A number is missing! Exit For 'Only need to find 1 error End If Next CheckLoop If IsValid Then 'Populate sheet ws.Range("A1").Value = OutputArray(1) ws.Range("C1").Value = OutputArray(2) ws.Range("E1").Value = OutputArray(3) ws.Range("A3").Value = OutputArray(4) ws.Range("C3").Value = OutputArray(5) ws.Range("E3").Value = OutputArray(6) ws.Range("A5").Value = OutputArray(7) ws.Range("C5").Value = OutputArray(8) ws.Range("E5").Value = OutputArray(9) 'Calculate sheet ws.Calculate 'Check if your output is correct If (False) Then GoTo QuickExit 'Replace (False) with your check! End If Next i, h, g, f, e, d, c, b, a 'No need for a Wall of "Next" QuickExit: Application.Calculation = xlCalculationAutomatic Application.ScreenUpdating = True SecondsElapsed = Round(Timer - StartTime, 2) MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation End Sub 是指针,而不是结构。这意味着您不能在其上使用Option Explicit Private ValueArray(1 To 9) As Long Private wb As Workbook Private ws As Worksheet Public Sub ControlLoop() Dim StartTime As Double Dim SecondsElapsed As Double StartTime = Timer Set wb = ThisWorkbook Set ws = wb.Worksheets("Sheet1") Application.Calculation = xlCalculationManual Application.ScreenUpdating = False Dim OutermostLoop As Long For OutermostLoop = 1 To 9 ClearArrayAbove 1 RecursiveArrayLoop 1, OutermostLoop Next OutermostLoop Application.Calculation = xlCalculationAutomatic Application.ScreenUpdating = True SecondsElapsed = Round(Timer - StartTime, 2) MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation End Sub Private Sub ClearArrayAbove(ArrayItem As Long) If ArrayItem >= 9 Then Exit Sub 'Safety check Dim InnerLoop As Long For InnerLoop = ArrayItem To 9 ValueArray(InnerLoop) = 0 Next InnerLoop End Sub Private Sub RecursiveArrayLoop(ArrayItem As Long, NewValue As Long) Dim InnerLoop As Long 'Number is not already in the array If IsError(Application.Match(NewValue, ValueArray, 0)) Then 'Add number to array ValueArray(ArrayItem) = NewValue If ArrayItem < 9 Then 'Go up a level, and loop again For InnerLoop = 1 To 9 ClearArrayAbove ArrayItem RecursiveArrayLoop ArrayItem + 1, InnerLoop Next InnerLoop Else 'All numbers filled! TestValidNumbers End If End If End Sub Private Sub TestValidNumbers() 'Populate sheet ws.Range("A1").Value = ValueArray(1) ws.Range("C1").Value = ValueArray(2) ws.Range("E1").Value = ValueArray(3) ws.Range("A3").Value = ValueArray(4) ws.Range("C3").Value = ValueArray(5) ws.Range("E3").Value = ValueArray(6) ws.Range("A5").Value = ValueArray(7) ws.Range("C5").Value = ValueArray(8) ws.Range("E5").Value = ValueArray(9) 'Calculate sheet ws.Calculate 'Check if your output is correct 'Do stuff here? End Sub

相反,您必须编写s(先引用,然后访问结构成员)或.(相同,但更短)。

此外,(*s).id s->id需要一个指针,因此应该是scanf,等等。

答案 1 :(得分:0)

我还发布了一个不使用指针的示例:

#include <stdio.h> 

typedef struct {
    int       id;
    float    grades[3];
} student_t;

student_t scan_student () {
    student_t s;
    printf("Please give patient's info:\n");
    scanf("%d%f%f%f", &s.id, &s.grades[0], &s.grades[1], &s.grades[2]);
    return s;
}

int main () 
{ 
    student_t stu2;

    stu2= scan_student();

    printf("Student's info are:\n");
    printf("%6d %5.2f %5.2f %5.2f\n", stu2.id, stu2.grades[0], stu2.grades[1], stu2.grades[2]);

    return 0; 
}