By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,985 Members | 1,863 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,985 IT Pros & Developers. It's quick & easy.

Returning a Two-D Array?

P: 4
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.

Expand|Select|Wrap|Line Numbers
  1. char ** setup_mutations()
  2. {
  3.     char mutations[256][MAX_STRING]; 
  4.     char ** result;// = malloc(sizeof(char));
  5.     int i,j;
  6.     char c, current, string[MAX_STRING];
  7.     FILE * fp;
  8.  
  9.  
  10.  
  11.     //result  = malloc(sizeof(char) * 256);
  12.  
  13.     if((fp = fopen("mutations.txt","r"))==NULL){
  14.         printf("Mutations file not available\n");
  15.         exit(2);
  16.     }
  17.  
  18.     for(i=0; i<256; i++){
  19.         //result[i] = malloc(sizeof(char));
  20.         mutations[i][0] = '\0';
  21.     }
  22.  
  23.     while ( (c = fgetc(fp) ) != EOF ){
  24.         current = c;
  25.  
  26.         for(c=fgetc(fp); c!=' '&&c!=EOF; c=fgetc(fp)){
  27.             ;
  28.         }
  29.  
  30.         i=0;
  31.  
  32.         for(c=c; c!='\n'&&c!=EOF; c=fgetc(fp)){
  33.             string[i] = c;
  34.             i++;
  35.         }
  36.         string[i] = '\n';
  37.         i = current;
  38.  
  39.         for(j=0; j!='\n'; j++){
  40.  
  41.                  mutations[i][j] = string[j]; // trying to put a string into a char
  42.         }
  43.     }
  44.  
  45.     if((fclose(fp))==-1)printf("Mutations file didn't close properly\n");
  46.  
  47.     result = mutations;
  48.     return result;
  49. };
I want to return mutations in the code above.

Thanks
Dec 8 '10 #1
Share this Question
Share on Google+
6 Replies


Expert 100+
P: 2,396
Take a look at Arrays Revealed.
Dec 8 '10 #2

Banfa
Expert Mod 5K+
P: 8,916
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.
Dec 8 '10 #3

P: 9
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
Dec 8 '10 #4

P: 3
Expand|Select|Wrap|Line Numbers
  1. char* Multiplications()
  2. {
  3.     char mutations[256][MAX_STRING]; 
  4. //....
  5.     return *mutations;
  6. }
  7.  
  8. int main()
  9. {
  10.     char (*c)[256][MAX_STRING] = (char(*)[256][MAX_STRING])Multiplications();
  11.     return 0;
  12. }
Dec 8 '10 #5

Expert 100+
P: 2,396
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.
Dec 8 '10 #6

P: 3
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..
Dec 8 '10 #7

Post your reply

Sign in to post your reply or Sign up for a free account.