Hello, I am getting an error when I run the following code: the error occurs in function printArryIIS when attempting to pring content of arrays, string pointer array to be specific:
Error msg: NON-FATAL RUN-TIME ERROR Attempt to read beyond end of string.
Code:
#include <ansi_c.h>
// Function prototypes
int initArryIIS(int *iP1,int *iP2, char *strP, int max);
int printArryIIS(int *p1, int *p2, char *strP, int max);
int main(void)
{
int i=0;
char str[20];
char * uLimit = malloc(144 * sizeof(char));
int *iPtr = (int *) malloc(10 * sizeof(int)); // allocate space for an array w/ 10 elements of type int
int *iPtr2 = (int *) malloc(10 * sizeof(int)); // allocate space for an array w/ 10 elements of type int
char *sPtr = (char *) malloc(10 * sizeof(char)); // allocate space for an array w/ 10 elements of type char
char *p2str, *p3str;
p2str = sPtr; // p2str now points to the base addresss of string array pointer sPtr
p3str = sPtr;
initArryIIS(iPtr,iPtr2,p2str,10);
p2str = sPtr;
printArryIIS(iPtr,iPtr2,p3str,10);
getchar();
}
int initArryIIS(int *iP1,int *iP2, char *strP, int max)
{
char str[15];
char *Ps;
Ps = strP;
for(int val=0; val<max; val++)
{
iP1[val] = val; // init int pointer array: method 1
*(iP2+val) = val+1; // init int pointer array: method 2
sprintf(str,"string%d\0",val); // init string pointer array
Ps =str;
++Ps;
}
return 0;
}
int printArryIIS(int *p1, int *p2, char *strP, int max)
{
for(int x=0; x<max; x++)
{
printf("[%d] %3d %3d %15s\n",x, *p1,*p2,strP); // NON-FATAL RUN-TIME ERROR: attempting to read beyond end of string???
++p1;
++p2;
++strP;
}
return 0;
}