C - 构建动态分配的指针数组,指向填充了文件输入的结构(C - Build dynamically allocated array of pointers to structures filled with input from file)
我需要构建一个指向动态分配结构(DBrecord)的指针数组,并用另一个文件的输入填充该数组。 不知道如何处理这个问题。
数据文件将首先具有条目数,然后是特定顺序的条目。
numOfEntries
lastName firstName studentID year gpa expGradYear
例:
1
Doe John 12345678 senior 3.14159 2015
这是我到目前为止的代码:
class.h
typedef enum {firstYear, sophomore, junior, senior, grad} class;
main.c中
#include <stdio.h> #include <stdlib.h> #include "class.h" int main(){ //DBrecord is name for structure struct DBrecord{ int DBrecordID; //ID for each entry, range 0-319 char *last; //student last name char *first; //student first name char studentID[8]; //student ID int age; //student age class year; //year in school float gpa; //GPA int expGradYear; //expected graduation year }; int numEntries; //total number of entries, first num in data file struct DBrecord **ptrToDB; //scan first int in data file and assign to numEntries scanf("%d", &numEntries); //allocate memory for structures, each is 36 bytes *ptrToDB = malloc (sizeof(struct DBrecord) * numEntries); //free allocated memory free(ptrToDB); //build an array of pointers to dynamically allocated structures //fill that array with input from data file //build 7 arrays of pointers to DBrecords, one for each field except DB ID //sort each a different way //note the 7 arrays are pointers, no copying //print each of the 7 sorted arrays return 0; }
I need to build an array of pointers to dynamically allocated structures (DBrecord) and fill that array with input from another file. Not sure how to approach this.
The data file will have the number of entries first, followed by entries in a specific order.
numOfEntries
lastName firstName studentID year gpa expGradYear
example:
1
Doe John 12345678 senior 3.14159 2015
Here's the code I have so far:
class.h
typedef enum {firstYear, sophomore, junior, senior, grad} class;
main.c
#include <stdio.h> #include <stdlib.h> #include "class.h" int main(){ //DBrecord is name for structure struct DBrecord{ int DBrecordID; //ID for each entry, range 0-319 char *last; //student last name char *first; //student first name char studentID[8]; //student ID int age; //student age class year; //year in school float gpa; //GPA int expGradYear; //expected graduation year }; int numEntries; //total number of entries, first num in data file struct DBrecord **ptrToDB; //scan first int in data file and assign to numEntries scanf("%d", &numEntries); //allocate memory for structures, each is 36 bytes *ptrToDB = malloc (sizeof(struct DBrecord) * numEntries); //free allocated memory free(ptrToDB); //build an array of pointers to dynamically allocated structures //fill that array with input from data file //build 7 arrays of pointers to DBrecords, one for each field except DB ID //sort each a different way //note the 7 arrays are pointers, no copying //print each of the 7 sorted arrays return 0; }
原文:https://stackoverflow.com/questions/10076261
满意答案
当你有多个链接在一起的
elsif
/elsif
块时,只有其中一个会运行,并且第一个具有真实条件的块将被运行。 所以,块的顺序很重要。 例如:if true puts 'this code will run' elsif true puts 'this code will not run' end
即使这些块的条件都是真的,只有第一个运行。 如果你想让两者都运行,使用两个独立的
if
块,如下所示:if true puts 'this code will run' end if true puts 'this code will also run' end
When you have multiple
if
/elsif
blocks chained together, only one of them will run, and the first block to have a true condition will be the one to be run. So, the order of the blocks matters. For example:if true puts 'this code will run' elsif true puts 'this code will not run' end
Even though the conditions for those blocks are both true, only the first one is run. If you want to have both run, use two separate
if
blocks, like this:if true puts 'this code will run' end if true puts 'this code will also run' end
相关问答
更多更改烧瓶中的URL路由会破坏代码吗?(Would changing URL route in flask break the code?)
python:在几个条件之后没有定义变量(python: variable not getting defined after several conditionals)
具有多个条件的if的执行顺序(Order of execution for an if with multiple conditionals)
我怎么推理Coq的条件?(How do I reason about conditionals in Coq?)
为什么改变条件的顺序会破坏我的代码?(Why does changing the order of conditionals break my code?)
重构条件(Refactoring conditionals)
PHP If / Else Multiple Conditionals(PHP If/Else Multiple Conditionals)
如何正确使用logstash条件(if ... in)?(How to use logstash conditionals(if…in) correctly?)
吞咽的条件(Conditionals in gulp)
以下条件是否相同(Are the following conditionals equivalent)
相关文章
更多zoj 2966 Build The Electric System
Nutch 1-build
My W3C Custom Mapping File
redhat6.4上build storm 0.9.0.1
【Hadoop】Build and Run HDFS
重新Build Hadoop后进入Hive客户端异常
用‘button’跟‘text’组合代替‘file’,选择文件后点‘submit’,‘file’的值被清空
《数据结构与STL》(Data Structures and the Standard Template Library)扫描版[PDF]
Java 流(Stream)、文件(File)和IO
shell 脚本执行,出现错误bad interpreter: No such file or directory
最新问答
更多您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
在计算机拍照在哪里进入
使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
单页应用程序:页面重新加载(Single Page Application: page reload)
在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!