S
santosh
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 of
allocated memory of fixed size (33 bytes). A unique 'string' is then
generated using itoa() and rand() for each block of memory.
Finally using pointer-to-pointer of type char, I print the contents of
the blocks of memory, i.e. the strings, using both printf() and
manually, character by character.
When trying to print character by character, the program is terminated
for a GPF. I suspect the fault is in the pointer manipulations between
lines 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 32
7: #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: else
23: 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 of
30: * allocated, zero'ed out memory, and pass it to itoa().
31: * Thus we get a block of pointers to type char, each pointing to
32: * a block of memory of size DEF_STR_SIZE, containing the character
33: * 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: else
42: {
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.
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 of
allocated memory of fixed size (33 bytes). A unique 'string' is then
generated using itoa() and rand() for each block of memory.
Finally using pointer-to-pointer of type char, I print the contents of
the blocks of memory, i.e. the strings, using both printf() and
manually, character by character.
When trying to print character by character, the program is terminated
for a GPF. I suspect the fault is in the pointer manipulations between
lines 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 32
7: #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: else
23: 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 of
30: * allocated, zero'ed out memory, and pass it to itoa().
31: * Thus we get a block of pointers to type char, each pointing to
32: * a block of memory of size DEF_STR_SIZE, containing the character
33: * 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: else
42: {
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.