Returning a Two-D Array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shane Fitz
    New Member
    • Nov 2010
    • 4

    Returning a Two-D Array?

    I need help returning a 2-d array from a function. I set up the array with no problems, but I want to know how to return it from the function. I was looking at pointer-to-pointer options but I'm confused now.

    Code:
    char ** setup_mutations()
    {
    	char mutations[256][MAX_STRING]; 
    	char ** result;// = malloc(sizeof(char));
    	int i,j;
    	char c, current, string[MAX_STRING];
    	FILE * fp;
    	
    	
    	
    	//result  = malloc(sizeof(char) * 256);
    	
    	if((fp = fopen("mutations.txt","r"))==NULL){
    		printf("Mutations file not available\n");
    		exit(2);
    	}
    	
    	for(i=0; i<256; i++){
    		//result[i] = malloc(sizeof(char));
    		mutations[i][0] = '\0';
    	}
    	
    	while ( (c = fgetc(fp) ) != EOF ){
    		current = c;
    		
    		for(c=fgetc(fp); c!=' '&&c!=EOF; c=fgetc(fp)){
    			;
    		}
    		
    		i=0;
    		
    		for(c=c; c!='\n'&&c!=EOF; c=fgetc(fp)){
    			string[i] = c;
    			i++;
    		}
    		string[i] = '\n';
    		i = current;
    		
    		for(j=0; j!='\n'; j++){
                     
    		         mutations[i][j] = string[j]; // trying to put a string into a char
            }
    	}
    	
    	if((fclose(fp))==-1)printf("Mutations file didn't close properly\n");
    	
    	result = mutations;
    	return result;
    };
    I want to return mutations in the code above.

    Thanks
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Take a look at Arrays Revealed.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Also your function is doomed to fail because on lines 47 and 48 you return a pointer to a variable with automatic scope and that will cease to exist as soon as the function exits.

      Comment

      • Raj K
        New Member
        • Dec 2010
        • 9

        #4
        1) Dont declare big memory on stack i.e. local variable (mutation in the above example).
        2)Use malloc to allocate the memory for result. And directly add to that memory instead of adding to first mutation then re return.
        3) Even if you want to use the mutation. In the last simply assignment operator will not work. You will have to copy the data from mutation to the memory which is allocated for result.


        - Raj
        Last edited by Niheel; Dec 9 '10, 03:27 PM.

        Comment

        • SAP88
          New Member
          • Oct 2009
          • 3

          #5
          Code:
          char* Multiplications()
          {
          	char mutations[256][MAX_STRING]; 
          //....
          	return *mutations;
          }
          
          int main()
          {
          	char (*c)[256][MAX_STRING] = (char(*)[256][MAX_STRING])Multiplications();
          	return 0;
          }
          Last edited by Niheel; Dec 9 '10, 03:27 PM. Reason: please use code tags

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            SAP88: do you intend Multiplications to return a pointer to the mutations array? That would cause all kinds of problems. The mutations array is declared as an automatic variable within Multiplications . All automatic variables from within Multiplications cease to exist when the function returns. Thus, you are returning a pointer to a nonexistent variable.

            Comment

            • SAP88
              New Member
              • Oct 2009
              • 3

              #7
              Its called once time, so cause a no dynamically allocated array will work fine. I has try to modify a code to having "char * setup_mutations ()" function prototype..

              Comment

              Working...