本文介绍了多个间接混乱......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好, 在下面的程序中,我分配一个指针块来键入char, 初始化为零。然后我将每个指针指向一个固定大小(33字节)的分配内存的块。然后使用itoa()和rand()为每个内存块生成一个唯一的''字符串'' 。 最后使用指针指向char类型的指针,我打印内存块的内容,即字符串,使用printf()和 手动逐字符打印。 当尝试逐字符打印时,程序终止 为GPF。我怀疑这个错误是在 第61和74行之间的指针操作。 有人能找到确切的错误吗? 代码如下: 1:/ *文件:006.c * / 2:#include< stdio.h> 3:#include< stdlib.h> 4:#include< time.h> 5: 6 :#define DEF_ALLOC_ITEMS 32 7:#define DEF_STR_SIZE 33 / *因为itoa()最多可以返回33个字节 * / 8: 9:int main(无效) 10:{ 11:char ** mptr = NULL; 12:char ** ptr = NULL; 13:char ** sptr = NULL; 14:int rnd; 15: size_t nitems = DEF_ALLOC_ITEMS; 16:size_t ctr; 17: 18:/ *分配一个指向char类型的指针数组为NULL * / 19:mptr = calloc(nitems,sizeof(char *)); 20:if(mptr == NULL) 21:退出(EXIT_FAILURE); 22:否则 23:ptr = mptr; 24: 25:/ *种子伪随机数生成器* / 26:srand((无符号)时间( NULL)); 27: 28:/ *使用itoa()和rand()作为参数来获取字符串。 29:*将每个分配的指针初始化为一块 30:*已分配,为零的内存,并将其传递给itoa()。 31 :*因此我们得到一个指向char类型的指针块,每个指向 32:*一块大小为DEF_STR_SIZE的内存块,包含字符 33:* string伪随机数的表示。 34:* / 35:nitems = DEF_STR_SIZE; 36:for(ctr = 0; ctr< DEF_ALLOC_ITEMS; ctr ++) 37:{ 38:* ptr = calloc(nitems,sizeof(char)); 39:if(* ptr == NULL) 40:退出(EXIT_FAILURE); 41:否则 42:{ 43 :rnd = rand(); 44:printf(字符串%u的\ n生成随机数:%d, 45:ctr + 1, rnd); 46:itoa(rnd,* ptr,10); 47:} 48:ptr ++; 49:} 50: 51:/ *现在打印字符串* / 52:ptr = mptr; 53:puts(" \\\Printing strings via printf():"); 54:for(ctr = 0; ctr< DEF_ALLOC_ITEMS; ctr ++) 55:{ 56:printf(" \ nString%u is:%s",ctr + 1,* ptr); 57:ptr ++; 58:} 59: 60:/ *现在使用多个间接手动打印字符串... * / 61:ptr = mptr; 62:sptr = ptr; 63:puts(" \ n\\\Printing字符串手动使用多个间接:); 64:for(ctr = 0; ctr< DEF_ALLOC_ITEMS; ctr ++) 65:{ 66:printf(" \ nString%u is:",ctr + 1); 67:sptr = ptr; 68:while(** sptr!=''\''') 69:{ 70:printf("%c",** sptr); 71:* sptr ++; 72:} 73:ptr ++ ; 74:} 75: 76:返回0; 77:} 谢谢。 Hi all, In the following program I allocate a block of pointers to type char,initialised to zero. I then point each of those pointers to a block ofallocated memory of fixed size (33 bytes). A unique ''string'' is thengenerated using itoa() and rand() for each block of memory. Finally using pointer-to-pointer of type char, I print the contents ofthe blocks of memory, i.e. the strings, using both printf() andmanually, character by character. When trying to print character by character, the program is terminatedfor a GPF. I suspect the fault is in the pointer manipulations betweenlines 61 and 74. Can anyone find the exact mistake? The code follows:1: /* File: 006.c */2: #include <stdio.h>3: #include <stdlib.h>4: #include <time.h>5:6: #define DEF_ALLOC_ITEMS 327: #define DEF_STR_SIZE 33/* Because itoa() can return upto 33 bytes*/8:9: int main( void )10: {11:char **mptr = NULL;12:char **ptr = NULL;13:char **sptr = NULL;14:int rnd;15:size_t nitems = DEF_ALLOC_ITEMS;16:size_t ctr;17:18:/* Allocate an array of pointers to type char, set to NULL */19:mptr = calloc(nitems, sizeof (char *));20:if(mptr == NULL)21:exit(EXIT_FAILURE);22:else23:ptr = mptr;24:25:/* Seed the pseudo-random number generator */26:srand((unsigned) time(NULL));27:28:/* Use itoa() with rand() as parameter to get a character string.29: * Initialise each of the allocated pointers to a block of30: * allocated, zero''ed out memory, and pass it to itoa().31: * Thus we get a block of pointers to type char, each pointing to32: * a block of memory of size DEF_STR_SIZE, containing the character33: * string representation of a pseudo-random number.34: */35:nitems = DEF_STR_SIZE;36:for(ctr = 0; ctr < DEF_ALLOC_ITEMS; ctr++)37:{38:*ptr = calloc(nitems, sizeof (char));39:if(*ptr == NULL)40:exit(EXIT_FAILURE);41:else42:{43:rnd = rand();44:printf("\nGenerated random number for string %u is: %d",45:ctr+1, rnd);46:itoa(rnd, *ptr, 10);47:}48:ptr++;49:}50:51:/* Now print the strings */52:ptr = mptr;53:puts("\nPrinting strings via printf():");54:for(ctr = 0; ctr < DEF_ALLOC_ITEMS; ctr++)55:{56:printf("\nString %u is: %s", ctr+1, *ptr);57:ptr++;58:}59:60:/* Now print the strings manually using multiple-indirection... */61:ptr = mptr;62:sptr = ptr;63:puts("\n\nPrinting strings manually using multiple-indirection:");64:for(ctr = 0; ctr < DEF_ALLOC_ITEMS; ctr++)65:{66:printf("\nString %u is: ", ctr+1);67:sptr = ptr;68:while(**sptr != ''\0'')69:{70:printf("%c", **sptr);71:*sptr++;72:}73:ptr++;74:}75:76:return 0;77: } Thanks. 推荐答案 - Eric Sosman es ***** @ acm-dot-org.inva lid --Eric Sosman es*****@acm-dot-org.invalid - Flash Gordon 住在有趣的时间。 虽然我的电子邮件地址说垃圾邮件,但它是真实的,我读了它。 --Flash GordonLiving in interesting times.Although my email address says spam, it is real and I read it. 谢谢。你是对的。我把优先权搞砸了。 我正在使用的这本书说如果在表达式中使用两个具有相同优先级的运算符 级别然后从左到右执行操作。 。如果是这样的话,那么上面的陈述应该有正确的工作,不应该吗?不是吗? 无论如何我把它改成了: (* sptr)++; 它工作正常。 Thanks. You''re right. I got the precedence messed-up. The book I''m using says that if two operators of the same precedencelevel are used in an expression, then operations are performed fromleft to right. If that''s so, then the above statement should haveworked correctly, shouldn''t it? Anyway I changed it to:(*sptr)++;It works correctly. - Eric Sosman -- Eric Sosman es ***** @ acm-dot-org.inva lid 这篇关于多个间接混乱......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-14 19:11