1
votes
#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.

4
You're dereferencing two indeterminate pointers, sending the results to a function that expects valid memory on which to write. You program thus invokes undefined behavior in every case where *lines or *linetwo appears as expressions. - WhozCraig
@WhozCraig I don't quite understand your comment :( I'm a beginner, so can you plz elaborate what it means to dereference two indeterminate pointers?? - deNsuh
@BLUEPIXY How are they incorrect?? - deNsuh
Neither of the pointers your sending to getline actually point to valid memory. That's what I mean. Your code declares something that can point to an array of 500 char (but doesn't), and a array of four pointers, each of which can point to char data of indeterminate length (but none do). In short, neither of the pointer values you're sending to getline actually point to anything concrete, yet getline treats them as if they do (because you told it so). - WhozCraig
@deNsuh Please allocate memory should point by pointer. - BLUEPIXY

4 Answers

3
votes

Your pointers don't point to anything, yet you use them as if they do. This is closer to what you likely need.

int main()
{
    char (*lines)[500] = malloc(sizeof *lines);
    char *linetwo[4] = { malloc(500) }; // other three pointers will be NULL

    getline(*lines, sizeof *lines);
    getline(*linetwo, 500); // or linetwo[0]

    printf("%s", *lines);
    system("PAUSE");

    free(lines);
    free(linetwo[0]);
    return 0;    
}

Note: no error checking performed above. Use at your own discretion. Also note your getline may-well conflict with the POSIX library function getline, which is a different issue entirely.

0
votes

To pass array of pointers in the function just pass the array name without using '*'

and that changes your function declaration with

void getline(char *line[], int lim)

and calling with

getline(linetwo, 500);

This is the easiest way to do so

0
votes
void getline(char *line, int lim)

This function expects a pointer.

You are passing array of pointer to it (*linetwo[4]) .Thus give an error.

To pass *linetwo[4] to pass this you can do this

void getline(char **line, int lim)
0
votes
char * lines[10]; 

Declares and allocates an array of pointers to char. Each element must be dereferenced individually.

char (* lines)[10]; /* invalid until assigned or malloc'ed) */ 

Declares (without allocating) a pointer to an array of char('s). The pointer to the array must be dereferenced to access the value of each element.