Returning A String Array

This is a discussion on Returning A String Array within the C Programming forums, part of the General Programming Boards category; Code: ......... char **words = readFile(in); printf("%s\n", words[0]); ......... char **readFile(FILE *in) { char **warr; ............ return warr; } Trying ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    6

    Returning A String Array

    Code:
    .........
    char **words = readFile(in);
    printf("%s\n", words[0]);
    .........
    
    char **readFile(FILE *in)
    {
    	char **warr;
            ............
    	return warr;
    }
    Trying to return the array and be able to print out what i read into the array. Read file works fine and i can print out the words in the array but having problems passing it back into another function after the call.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    ....but having problems...
    I hate when that happens.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    Could someone help me out please?

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    You are returning the address of a locally declared variable. If you want to get information from warr out of the function, you need to pass it in as a parameter to the function after declaring it elsewhere.

    I think.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    FILE *in;
    char **readFile();
    
    void main()
    {
    	in = fopen("sentences.in", "rt");
    	char **words;
    	words = readFile(in);
    	printf("%s\n", words[0]);
    }
    
    char **readFile(FILE *in)
    {
    	char *word, **warr;
    	word = (char*)malloc(25 * sizeof(char));
    	for(int i = 0; i < 28; ++i)
    	{
    		fscanf(in, "%s", word);
    		if(i >= 28)
    		{
    			printf("Only %d words can be used in your input file.", 20);
    		}
    		warr[i] = calloc(strlen(word) + 1, sizeof(char));
    		strcpy(warr[i], word);
    	}
            printf("%s\n", words[0]);
    	return warr;
    }
    Prints word from input file in readFile but doesn't do it in main after the return. It's not being pass correctly and I'm not sure how to do it :/

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Not too bad, but you first need to allocate warr, before using warr[i]

    For your specific program at the moment, this would be
    Code:
    warr = malloc( 28 * sizeof(*warr) );
    > if(i >= 28
    This is never true inside a for loop with the opposite condition controlling it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    Quote Originally Posted by Salem View Post
    Not too bad, but you first need to allocate warr, before using warr[i]

    For your specific program at the moment, this would be
    Code:
    warr = malloc( 28 * sizeof(*warr) );
    > if(i >= 28
    This is never true inside a for loop with the opposite condition controlling it.
    I did that I just put the code that refers to word since that is what I'm having trouble passing :/

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    111

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > I did that I just put the code that refers to word since that is what I'm having trouble passing :/
    So in other words, this thread is a waste of time, because your actual code is nothing like your posted code.

    Post your ACTUAL code if you want people to tell you what is actually wrong.
    Otherwise, we just waste time pointing out what is wrong with your attempt to be brief.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    Salem, why aren't you complaining about void main?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    This thread started out sloppy, and is still sloppy. Sigh....
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM

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