#include <stdio.h>
#include <stdlib.h>
void getline(char *line, int lim)
{
int len = 0;
char c;
while (len < lim)
{
if ((c=getchar()) != EOF && c != '\n')
{
*line++ = c;
printf("reading %c\n", c);
len++;
}
else
break;
}
*line = '\0';
}
int main()
{
char (*lines)[500]; // pointer to array with 500 chars
char *linetwo[4]; //why doesnt this work???? array of 4 pointers.
getline(*lines, 500);
getline(*linetwo, 500); // !!!!ERROR!!!
printf("%s", *lines);
system("PAUSE");
return 0;
}
I'm having trouble with this code. I want four lines of input with each lines having maximum 500 chars. I wrote a getline function to save it to a char * pointer. However, getline gives error when I initialize an array of pointers.
The only difference between (*lines)[500] and *lines[4] is, I think, whether it does not specify either the number of lines or the number of chars in a line.
Please help me understand why passing *linetwo into getline after *linetwo[4] initialization gives error.
*linesor*linetwoappears as expressions.getlineactually point to valid memory. That's what I mean. Your code declares something that can point to an array of 500char(but doesn't), and a array of four pointers, each of which can point tochardata of indeterminate length (but none do). In short, neither of the pointer values you're sending togetlineactually point to anything concrete, yetgetlinetreats them as if they do (because you told it so).