“指向”存储在内存中的另一个值的数据类型。指针变量包含某个其他实体(变量或函数或其他实体)的内存地址。此标记应用于涉及指针使用的问题,而不是引用。使用指针的最常见编程语言是C,C ++,Go和汇编语言。使用特定语言标记。其他有用的标签是描述指针使用的方法,函数,结构等。
重点是让它与指针一起工作。另外,我在 C++ 中也发现了类似的问题。它在 FreePascal 中不起作用,好吧,我可能错过了一些东西。 我为我的指针声明类型和变量:...
不兼容的指针类型使用“BaseClass *”类型的表达式初始化“SubClass *__strong”
在objective-C中,为什么我们不能用超类alloc+init或者new一个基类对象,而我们可以使用超类的构造函数来初始化? 下面是一些代码: s1 可以被创建相当
我正在用 C 语言进行大学项目,遇到了一些问题。 我使用指向结构的指针,并使用 fwrite 将其写入文件,但它没有帮助。这是我使用的代码。 #包括 我正在用 C 语言进行大学项目,遇到了一些问题。 我使用了结构体指针,并使用 fwrite 将其写入文件,但这没有帮助。这是我使用的代码。 #include<stdio.h> #include<stdlib.h> #include<ctype.h> #include<string.h> #include<conio.h> struct collection{ char *fname, *lname, *telephone, *address, *seat; }; collection * alloc( ){ struct collection *r = (struct collection *) malloc (sizeof(collection*)); r->fname = NULL; r->lname = NULL; r->telephone = NULL; r->address = NULL; r->seat = NULL; return (r); } void string_realloc_and_copy (char **dest, const char *src){ *dest =(char *) realloc (*dest, strlen (src) + 1); strcpy (*dest, src); } int main(){ char ch = 'Y', temp[50]; FILE *ptf; struct collection *asd; asd = alloc(); //printf("%d",sizeof(asd)); //opening file ptf = fopen("lang.txt","w+"); do{ printf("First name: "); gets(temp); string_realloc_and_copy(&asd->fname,temp); printf("Last name: "); gets(temp); string_realloc_and_copy(&asd->lname,temp); printf("Telephone: "); gets(temp); string_realloc_and_copy(&asd->telephone,temp); printf("Address: "); gets(temp); string_realloc_and_copy(&asd->address,temp); printf("Seat you want to book: "); gets(temp); string_realloc_and_copy(&asd->seat,temp); fwrite(asd,12*sizeof(collection),1,ptf); fflush(ptf); //fprintf(ptf,"\n"); printf("Do you wish to enter another data...? (Y/N) "); ch = getch(); }while((ch=toupper(ch))== 'Y'); rewind(ptf); while(fread(asd,12*sizeof(collection),1,ptf) == 1){ printf("\n\n%s",asd->fname); printf("\n\n%s",asd->lname); printf("\n\n%s",asd->telephone); printf("\n\n%s",asd->address); printf("\n\n%s",asd->seat); } fclose(ptf); } 它会一直工作,直到到达asd->电话,询问地址并且没有响应。我不明白我做错了什么。我以为是内存不足所以我改变了 struct collection *r = (struct collection *) malloc (sizeof(collection*)); 到 struct collection *r = (struct collection *) malloc (12*sizeof(collection*)); 它工作了一段时间,同样的事情又发生了。我使用 devC++ 进行编译。预先感谢; 首先,您不应该使用 gets(),因为它已被弃用,并且它从 stdin 获取的字符数量没有限制。我建议你使用 fgets。你应该像这样使用 malloc 作为指针 malloc(sizeof(*collection)); 通过这种方式,您可以为指针分配内存。 另一件事是用 putc() 尝试这个程序,看看它是否有效。 malloc (sizeof(collection*)) 应该是malloc(sizeof(*collection))。您的代码仅为指向 collection 类型的指针分配足够的空间,而不是 collection 指针指向的结构的大小。 这也说明了为什么对变量和类型使用相同的名称是一个坏主意。如果您使用不同的名称,编译器就会出现错误。使用 collection_t 作为类型。 尝试 sizeof (struct collection) 而不使用 * 我发现您的代码中有很多更改。第一个更改是struct collection而不是collection。第二个更改是我使用了两个文件描述符,一个用于读取另一个用于写入。您正在初始化的代码中的第三个更改“asd”仅一次,因此它每次都会将相同的结构写入文件中。所以我在 do-while 循环中进行了初始化。第四个我使用了 scanf 而不是 gets,因为当你接受多个输入时,它会跳过一些输入。第五个我删除了 12 个形式的 fwrite 和 fread。第六个我使用了一个 getchar,因为我们将给出座位号后按 Enter 键,这样 ch 就会采用该 Enter 来获取用户输入,我必须再使用一个 getchar最后更改的代码是 #include<stdio.h> #include<stdlib.h> #include<ctype.h> #include<string.h> //#include<conio.h> struct collection { char *fname, *lname, *telephone, *address, *seat; }; struct collection *alloc( ){ struct collection *r= malloc(sizeof(struct collection)); r->fname = NULL; r->lname = NULL; r->telephone = NULL; r->address = NULL; r->seat = NULL; return (r); } void string_realloc_and_copy (char **dest, const char *src){ *dest =(char *) realloc (*dest, strlen (src) + 1); strcpy (*dest, src); } int main(){ char ch = 'Y', temp[50]; FILE *ptf; struct collection *asd; //printf("%d",sizeof(asd)); //opening file ptf = fopen("lang.txt","wb"); do{ asd = alloc(); printf("First name: "); scanf("%s",temp); string_realloc_and_copy(&asd->fname,temp); printf("Last name: "); scanf("%s",temp); string_realloc_and_copy(&asd->lname,temp); printf("Telephone: "); scanf("%s",temp); string_realloc_and_copy(&asd->telephone,temp); printf("Address: "); scanf("%s",temp); string_realloc_and_copy(&asd->address,temp); printf("Seat you want to book: "); scanf("%s",temp); string_realloc_and_copy(&asd->seat,temp); fwrite(asd,sizeof(struct collection),1,ptf); fflush(ptf); //fprintf(ptf,"\n"); printf("Do you wish to enter another data...? (Y/N) "); ch = getchar(); ch = getchar(); }while((ch=toupper(ch))== 'Y'); //rewind(ptf); fclose(ptf); FILE *ptf1; ptf1 = fopen("lang.txt","rb"); while(fread(asd,sizeof(struct collection),1,ptf1)){ printf("\n\n%s",asd->fname); printf("\n\n%s",asd->lname); printf("\n\n%s",asd->telephone); printf("\n\n%s",asd->address); printf("\n\n%s",asd->seat); } fclose(ptf1); } 样品输出是 遗憾的是,这是完全错误的指针应用。只是,分开两个函数 - 在两个不同的源文件中写入和读取,然后亲自查看。
我试图理解这两段C 代码之间的区别: int 数组[] = { 1, 2, 3 }; // 初始化一个常规数组 int *numList = {1, 2, 3}; // 初始化一个指针数组? 我在...
这个极其简单的攻击游戏(对我来说)的主要价值是熟悉简单的多态性,并练习使用指针。话虽如此,我很想补充一下。寻找...
我正在阅读有关 cpython 的 list.pop(k) 方法的实现,并了解到删除列表中的第 k 个元素需要 O(n-k) 时间,因为它需要将元素向左移动一位。
在 Deitel 的书《C. 如何编程》(第 7 章)的第 9 版中,函数被声明为 void bubbleSort(int * const 数组, size_t 大小) 并在文本中指定“函数气泡...
错误是:从不兼容的指针类型[-Win兼容指针类型]传递'init_node'的参数5
char initial_req_item[MAX_ANSWER_COUNT][10] = {" ", "свиня", "меч"}; int initial_req_val[MAX_ANSWER_COUNT] = {0, 1, 1}; int is_initial_req[MAX_ANSWER_COUNT] = ...
在HTSlib中,Bonfield内存将一系列方法对齐到32。它为什么这样做,它实现了什么? 因此,例如这里有这样的代码: 如果(类型=='c'){ 如果 (!(q =
我想从一个源中检索数据,这些数据通过一个函数、一组不同的函数提供。我来自c,所以我有这个例子: #包括 使用命名空间 std; ...
我有一个问题,似乎kernel6在直接打印指针和传输到整数之间有不同的指针值。 我的机器 # uname-srvmo Linux 6.8.0-51-generic #52-Ubuntu SMP PREEMPT_DYNAMIC Th...
在我的 C++ 项目中,所有类都使用链表类,当我运行该项目时,它给了我 nullptr 异常如图所示 该函数添加到 head 及其错误 我尝试用
为什么 C 中 int 变量需要“&”运算符,而数组不需要?
#包括 #包括 int main(){ 字符名称[100]; 年龄; printf("请输入您的姓名: ”); scanf("%s", 名称); printf("输入y...
我想了解最后一行的作用: int PCKT_LEN = 8192; 字符缓冲区[PCKT_LEN]; 结构 iphdr *ip = (结构 iphdr *) 缓冲区; 结构 udphdr *udp = (结构 udphdr *) (缓冲区 +
我查看了有关此主题的许多类似问题,但大多数似乎都集中在作为参数传递给函数的指针而不是指针的地址,因此还没有找到我的答案。 段错误
我认为从原始指针转换为唯一指针一点也不难。然而,当我尝试为自己做一个时,我遇到了很多我什至不知道的问题......
有这段代码正在选择我想要保留的点。 本征::Vector3f 罗德里格斯(0,0,0); 特征::Vector3f boxCenter(0.5, 0.5, 0.5); 特征::Vector3f box_size(0.9, 0.9 , 0.9); ...
在课堂上,我必须编写使用结构数组的简单程序,该程序需要添加元素并显示一个或所有元素。老师说不能用向量,必须用