Thread: Returning A String Array

  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
    Chappell Hill, Texas
    Posts
    2,332
    ....but having problems...
    I hate when that happens.
    Mainframe assembler programmer by trade. C coder when I can.

  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
    486
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > 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.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    Chappell Hill, Texas
    Posts
    2,332
    This thread started out sloppy, and is still sloppy. Sigh....
    Mainframe assembler programmer by trade. C coder when I can.

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, 05: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, 03:23 PM