Returning a two-dimensional array in a function

This is a discussion on Returning a two-dimensional array in a function within the C Programming forums, part of the General Programming Boards category; I'm having trouble writing a function that reads a file into an array... just like PHPs file function here file ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    7

    Returning a two-dimensional array in a function

    I'm having trouble writing a function that reads a file into an array... just like PHPs file function here file

    Oh but first I cant even get the two-dimensional array to populate correctly as you can see I'm doing a test at the end of the function with a for loop.

    And I really cant get this to work any way I try...any suggestions?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #include "file.h"
    
    char ** file(const char * filename) {
    	FILE *pFile;
    	pFile = fopen(filename, "r");
    	
    	/* line count */
    	int lc = 0;
    	char **lines;
    	char **new_lines;
    	lines = (char **) calloc(1, sizeof(char));
    	
    	while (!feof(pFile)) {
    		fgets(lines[lc], 255, pFile);
    		new_lines = realloc(lines, 2 * sizeof(char));
    		if (new_lines != NULL) {
    			lines = new_lines;
    		} else {
    			break;
    		}
    		lc++;
    	}
    	
    	int i;
    	for (i = 0; i < 2; i++) {
    		printf("\n%s", lines[i]);
    	}
    	
    	return lines;
    }
    Last edited by neptunusmaris; 07-02-2009 at 05:30 AM. Reason: fixed hyperlink

  2. #2
    Registered User
    Join Date
    Jul 2009
    Posts
    40
    Don't forget to capture the problem that might occur when opening a file:

    Code:
    if( ( pFile = fopen(filename, "r") == NULL )
    { 
       //error message
       exit( 1 );
    }

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    7
    Quote Originally Posted by WatchTower View Post
    Don't forget to capture the problem that might occur when opening a file:

    Code:
    if( ( pFile = fopen(filename, "r") == NULL )
    { 
       //error message
       exit( 1 );
    }

    Oh... yeah I know that... but I was just working on trying to return a two-dimensional array, but thanks for the response!

  4. #4
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    > lines = (char **) calloc(1, sizeof(char));
    lines is a pointer to a pointer to a char, and you are allocating the memory for a char not for a char*.
    it should be:
    Code:
    lines=(char**)calloc(1, sizeof(char*));

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Another problem:
    Code:
    new_lines = realloc(lines, 2 * sizeof(char));
    1) this should be *char again.
    2) You are not increasing the size here; lines/new_lines will remain the same size each time (2* sizeof(char)); probably you want (lc+1)*sizeof(char*). If you do that *before* the fgets, you won't need the initial calloc at all (you can use realloc without using malloc or calloc first).

    Most biggest serious problem: You do not allocate anything at all for each line.
    "**array" has two dimensions. You are obviously (attempting) to allocate for the first dimension, which is the number of lines. But those lines do not have any length. You need to do this:
    Code:
    lines[lc] = malloc(255);
    *before* fgets.
    Last edited by MK27; 07-02-2009 at 07:25 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    Code:
    char **lines = (char **) calloc(1, sizeof(char *));
    //add error checking
    for (int i = 0; i < 1; i++)
     //could do it without the loop obviously, but you can take this example and //generalize it
    {
          lines[i] = calloc(255, sizeof(char));
          //add error checking
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 05-29-2009, 05:48 AM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21