Hello.
I'm having a difficult time figuring out how to set the plines[n] pointer. It looks like the realloc is working in the sub program, but I'm not having luck with printing it out in the main program.
On compile, I get, "Suspicious pointer conversion in function readfile". Basically, this means the pointer is not "set" on strdup(). Can somebody please take a look at this and help me with this pointer problem?
Thanks!
Code:
/* dynline2.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int readfile(char ***plines);
int main(void){
char **plines; /* ptr to ptr array */
int n, k; /* counters */
n = readfile(&plines);
/* test prints */
printf("\nfmain:\nplines-*>%p\nplines-s>%s\n n->%d\n", plines, plines[n], n);
for(k=0; k<n; k++){
puts(plines[k]); /* print line */
free(plines[k]); /* free storage */
}
free(plines); /* free storage */
return 0;
}
int readfile(char ***plines){
const int hunk=10; /* increment of linesize */
char *line; /* input buffer */
char *tmp1, ***tmp2; /* temporaries */
int linesize; /* current size of line input area */
int n; /* counters */
line=(char *)malloc(hunk); /* allocate space for input line */
if (line==NULL) { perror("dynline2"); exit(1); }
linesize = hunk;
plines=NULL;
for(n=0;;n++){
if(feof(stdin)) break; /* if EOF then no more lines */
line[0]='\0';
for(;;){
tmp1 = fgets(line+strlen(line), linesize-strlen(line), stdin);
if (tmp1==NULL) break;
if ( line[-1+strlen(line)] == '\n'){/*if \n then end of line */
strtok(line, "\n"); /* discard \n */
break;
}
linesize = linesize + hunk;
tmp1=(char *)realloc(line, linesize); /* storage for ptr */
if ( tmp1==NULL ) { perror("dynline2"); exit(1); }
else line=tmp1;
}
tmp2=(char ***)realloc( plines, (n+1)*(sizeof(char ***)) );
if (tmp2==NULL) { perror("dynline2"); exit(1); }
else plines=tmp2;
plines[n]=strdup(line); /* allocate space for ptr */
puts("test print plines[n]:");
puts(plines[n]);
/* test prints */
puts("freadfile:");
printf("plines-*>%p \n tmp2-*>%p \nplines-s>%s \n tmp2-s>%s\n", plines, tmp2, plines, tmp2);
printf(" line-*>%p \n tmp1-*>%p \n line-s>%s \n tmp1-s>%s\n", line, tmp1, line, tmp1);
if (plines[n]==NULL) { perror("dynline2"); exit(1); }
}
free(line);
return n;
}
compile:
C:\BORLAND\BCC55\BIN>bcc32 -odynline2 dynline2.c
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
dynline2.c:
Warning W8075 dynline2.c 57: Suspicious pointer conversion in function readfile
Warning W8075 dynline2.c 59: Suspicious pointer conversion in function readfile
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
output:
C:\BORLAND\BCC55\BIN>dynline2
hello world how are you?
test print plines[n]:
hello world how are you?
freadfile:
plines-*>007C2750
tmp2-*>007C2750
plines-s>`'|
tmp2-s>`'|
line-*>007C272C
tmp1-*>007C273F
line-s>hello world how are you?
tmp1-s> you?
I'm having difficulty figuring this sucker out?
test print plines[n]:
I'm having difficulty figuring this sucker out?
freadfile:
plines-*>007C2750
tmp2-*>007C2750
plines-s>`'|
tmp2-s>`'|
line-*>007C2780
tmp1-*>007C27A7
line-s>I'm having difficulty figuring this sucker out?
tmp1-s>ker out?
What is the problem?
test print plines[n]:
What is the problem?
freadfile:
plines-*>007C2750
tmp2-*>007C2750
plines-s>`'|
tmp2-s>`'|
line-*>007C2780
tmp1-*>007C2780
line-s>What is the problem?
tmp1-s>What is the problem?
^Z
test print plines[n]:
freadfile:
plines-*>007C280C
tmp2-*>007C280C
plines-s>`'|
tmp2-s>`'|
line-*>007C2780
tmp1-*>00000000
line-s>
tmp1-s>(null)
fmain:
plines-*>007C21CC
plines-s>(null)
n->4