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

7
  • 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
    Jul 11, 2015 at 17:48
  • @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
    Jul 11, 2015 at 17:52
  • @BLUEPIXY How are they incorrect??
    – deNsuh
    Jul 11, 2015 at 17:53
  • 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
    Jul 11, 2015 at 17:55
  • @deNsuh Please allocate memory should point by pointer.
    – BLUEPIXY
    Jul 11, 2015 at 17:56

4 Answers 4

3

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.

5
  • thanks a lot! I now kind of get the idea of memory allocating and dereferencing and stuff :)
    – deNsuh
    Jul 11, 2015 at 18:09
  • @deNsuh so long as you understand the difference between a pointer to an array of length N (lines in your code), and an array of length N of pointers (linetwo in your code), you'll probably be ok. Its a hurdle for beginning C programmers. Keep at it.
    – WhozCraig
    Jul 11, 2015 at 18:12
  • Not a beginner but still learning. I had thought the two char declarations were synonymous (apart from the 500 and the 4). But when I print the sizeof each one, they are 4 and 16. Jul 11, 2015 at 18:26
  • @WeatherVane yeah, they're not synonymous, as you can see. One declares a pointer (singular) to type char[500], the other declares an array of four pointers (plural) to a specific type char. As I said, it's part of the set of hurdles beginners have to get over when learning C, pointers, etc. It can certainly be confusing at-first, to be sure.
    – WhozCraig
    Jul 11, 2015 at 18:32
  • After 25 years or so I still would not use char (*lines)[500], I would keep it simple and use char *line in the singular. Since getline() doesn't know the length, without the additional argument, what's the point? Jul 11, 2015 at 18:41
0

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
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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.