handling 2D array..

This is a discussion on handling 2D array.. within the C Programming forums, part of the General Programming Boards category; i got an array char f[3][20]; so when i am doing Code: fputs(f[equal],common); why i am getting error C2107: illegal ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    handling 2D array..

    i got an array
    char f[3][20];

    so when i am doing
    Code:
    fputs(f[equal],common);
    why i am getting
    error C2107: illegal index, indirection not allowed
    my code is:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define false 0
    #define true 1
    
    char* equal(char*f1,char*f2,char*f3);
      int smallest(char*f1,char*f2,char*f3);
    int main()
    {
    
    	FILE *file[3];
    	FILE *common;
    	
    	char f[3][20];
    	 
    	int small;
        char* equall;	 
    	file[1]=fopen("c:\\1.txt","r");
    	file[2]=fopen("c:\\2.txt","r");
    	file[3]=fopen("c:\\3.txt","r");
    	common=fopen("c:\\common.txt","w");
    	fscanf(file[1],"%9s",f[1]);
    	fscanf(file[2],"%9s",f[2]);
    	fscanf(file[3],"%9s",f[3]);
    	while(fgetc(file[1])!=NULL||fgetc(file[2])!=NULL||fgetc(file[3])!=NULL)
    	{
    	    small=smallest(f[1],f[2],f[3]);
    	    equall=equal(f[1],f[2],f[3]);
    		
    		fputs(f[equal],common);
    		fputc('\n',common);
    		fscanf(file[small],"%20s",f[small]);
    	} 
    
    	fclose(file[1]);
    	fclose(file[2]);
    	fclose(file[3]);
    	return 0;
    }
    
    int smallest(char*f[1],char*f[2],char*f[3])
    {
         int i=1;
    	 char* pLowest = f[1];
         if(strcmp(pLowest, f[2]) > 0)
    		 i=2;
          pLowest = f[2];
          if(strcmp(pLowest, f3) > 0)
           pLowest = f[3];
    	  i=3;
          return i;
    }
    
      int equal(char*f1,char*f2,char*f3)
      {
    	   
         if(strcmp(f1,f3)==0)
    	 {
           return  1;
    	 }
         if(strcmp(f2,f3)==0)
    	 {
           return  2;
    	 }
    	  return NULL;
      }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I think you meant equall, not equal.
    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

  3. #3
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    i get a whole lot more of errors and warning...you better check them too.
    Code:
    [c_d@localhost C scratchpad]$ gcc temp.c
    temp.c: In function ‘main’:
    temp.c:26: warning: comparison between pointer and integer
    temp.c:26: warning: comparison between pointer and integer
    temp.c:26: warning: comparison between pointer and integer
    temp.c:31: error: array subscript is not an integer
    temp.c: At top level:
    temp.c:42: error: redefinition of parameter ‘f’
    temp.c:42: error: previous definition of ‘f’ was here
    temp.c:42: error: redefinition of parameter ‘f’
    temp.c:42: error: previous definition of ‘f’ was here
    temp.c:42: error: conflicting types for ‘smallest’
    temp.c:8: error: previous declaration of ‘smallest’ was here
    temp.c: In function ‘smallest’:
    temp.c:49: error: ‘f3’ undeclared (first use in this function)
    temp.c:49: error: (Each undeclared identifier is reported only once
    temp.c:49: error: for each function it appears in.)
    temp.c: At top level:
    temp.c:55: error: conflicting types for ‘equal’
    temp.c:7: error: previous declaration of ‘equal’ was here
    temp.c: In function ‘equal’:
    temp.c:66: warning: return makes integer from pointer without a cast
    [c_d@localhost C scratchpad]$
    isnt equal a function? if you want it to be the index then

    it should be

    f[equal(parameters)] and since equal() returns int it wouldnt be any problems
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    ok i solved this error but still i have this 2d array problem
    c(42) : error C2086: 'char **f' : redefinition
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define false 0
    #define true 1
    
    char* equal(char*f1,char*f2,char*f3);
      int smallest(char*f1,char*f2,char*f3);
    int main()
    {
    
    	FILE *file[3];
    	FILE *common;
    	
    	char f[3][20];
    	 
    	int small;
        int equall;	 
    	file[1]=fopen("c:\\1.txt","r");
    	file[2]=fopen("c:\\2.txt","r");
    	file[3]=fopen("c:\\3.txt","r");
    	common=fopen("c:\\common.txt","w");
    	fscanf(file[1],"%9s",f[1]);
    	fscanf(file[2],"%9s",f[2]);
    	fscanf(file[3],"%9s",f[3]);
    	while(fgetc(file[1])!=NULL||fgetc(file[2])!=NULL||fgetc(file[3])!=NULL)
    	{
    	    small=smallest(f[1],f[2],f[3]);
    	    equall=equal(f[1],f[2],f[3]);
    		
    		fputs(f[equall],common);
    		fputc('\n',common);
    		fscanf(file[small],"%20s",f[small]);
    	} 
    
    	fclose(file[1]);
    	fclose(file[2]);
    	fclose(file[3]);
    	return 0;
    }
    
    int smallest(char* f[1],char* f[2],char* f[3])
    {
         int i=1;
    	 char* pLowest = f[1];
         if(strcmp(pLowest, f[2]) > 0)
    		 i=2;
          pLowest = f[2];
          if(strcmp(pLowest, f[3]) > 0)
           pLowest = f[3];
    	  i=3;
          return i;
    }
    
      int equal(char*f1,char*f2,char*f3)
      {
    	   
         if(strcmp(f1,f3)==0)
    	 {
           return  1;
    	 }
         if(strcmp(f2,f3)==0)
    	 {
           return  2;
    	 }
    	  return NULL;
      }

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Probably because of this:
    Code:
    char* equal(char*f1,char*f2,char*f3);
      int smallest(char*f1,char*f2,char*f3);
    or rather
    Code:
    int smallest(char* f[1],char* f[2],char* f[3])
    which I think you want the first version throughout.
    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
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    i think you meant f1 f2 f3 instead of f[1] f[2] f[3] as you declared in the function on top

    also there are a lot of uncasted typeconversions...the compiler is not very happy...check those up too...
    Last edited by creeping death; 03-29-2009 at 12:56 PM. Reason: grammer error
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    but its only a parameter
    we can put
    Code:
    int smallest(char* f[1],char* f[2],char* f[3])
    Code:
    int smallest(char* bm,char* bjk,char* jjjjj)
    when i use the function i would write
    Code:
    t=smallest(f[1],f[2],f[3]);
    it doesnt matter what names i put in the signature
    ??

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    it doesnt matter what names i put in the signature
    as long as i use them inside the function

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by transgalactic2 View Post
    it doesnt matter
    If you say so -- maybe the compiler has a programming problem and should start a new thread about warnings/errors from transgalactic2?

    It does matter, dude. You can call the variables different names (or use no name at all) in the prototype but The Types Must Match, and here they do not.
    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

  10. #10
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    Quote Originally Posted by transgalactic2 View Post
    Code:
    int smallest(char*f1,char*f2,char*f3);
    Code:
    int smallest(char* f[1],char* f[2],char* f[3])
    In your forward declaration:
    char* f1 is declaring f1, which is a pointer to char.
    char* f2 is declaring f2, which is a pointer to char.
    char* f3 is declaring f3, which is a pointer to char.

    In your function definition:
    char* f[1] is declaring f, which is an array of pointers to char.
    char* f[2] is also declaring f, which is an array of pointers to char. And
    char* f[3] is also declaring f, which is an array of pointers to char.

    You are correct that your forward declaration and your function definition do not need to use the same variable names, but they must use the same types. Because you added the brackets [] around the 1, 2 and 3, you changed the types. Remember, a variable name can only contain letters, numbers and the underscore.
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  11. #11
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    like this i get an error too
    "Remember, a variable name can only contain letters, numbers and the underscore."
    but i get
    .c(42) : error C2065: 'c' : undeclared identifier
    1>c:\documents and settings\\my documents\visual studio 2005\projects\ex6\ex6\ex6.c(42) : error C2057: expected constant expression
    1>c:\documents and settings\\my documents\visual studio 2005\projects\ex6\ex6\ex6.c(42) : error C2466: cannot allocate an array of constant size 0
    1>c:\documents and settings\\my documents\visual studio 2005\projects\ex6\ex6\ex6.c(42) : error C2086: 'char **f' : redefinition
    1> c:\documents and settings\\my documents\visual studio 2005\projects\ex6\ex6\ex6.c(42) : see declaration of 'f'
    Code:
    int smallest(char* f[a],char* f[b],char* f[c])
    {
         int i=1;
    	 char* pLowest = f[a];
         if(strcmp(pLowest, f[b]) > 0)
    		 i=2;
          pLowest = f[b];
          if(strcmp(pLowest, f[c]) > 0)
           pLowest = f[c];
    	  i=3;
          return i;
    }

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    It amazes me that even with your long time of asking questions and racking up an impressive 1245 posts doing so, you still haven't fixed your indentation or grasp the very basics.
    Of C or posting replies, for that matter.
    Pointing out the lines of error may help too.

    And posting the entire source.
    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.

  13. #13
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    Quote Originally Posted by transgalactic2 View Post
    but i get
    .c(42) : error C2065: 'c' : undeclared identifier
    Code:
    int smallest(char* f[a],char* f[b],char* f[c])
    {
         int i=1;
    	 char* pLowest = f[a];
         if(strcmp(pLowest, f[b]) > 0)
    		 i=2;
          pLowest = f[b];
          if(strcmp(pLowest, f[c]) > 0)
           pLowest = f[c];
    	  i=3;
          return i;
    }
    The compiler doesn't understand that you mean to declare 3 separate variables as arguments to the function. It only sees that you are declaring f 3 times in a row, each time defining it as an array of pointers to char.

    Do you understand the difference between these two declarations?
    Code:
    char* f1;
    char* f[1];
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  14. #14
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Note that array indexes in C start at 0 not 1.

  15. #15
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    Quote Originally Posted by transgalactic2 View Post
    like this i get an error too
    "Remember, a variable name can only contain letters, numbers and the underscore."
    but i get
    .c(42) : error C2065: 'c' : undeclared identifier
    1>c:\documents and settings\\my documents\visual studio 2005\projects\ex6\ex6\ex6.c(42) : error C2057: expected constant expression
    1>c:\documents and settings\\my documents\visual studio 2005\projects\ex6\ex6\ex6.c(42) : error C2466: cannot allocate an array of constant size 0
    1>c:\documents and settings\\my documents\visual studio 2005\projects\ex6\ex6\ex6.c(42) : error C2086: 'char **f' : redefinition
    1> c:\documents and settings\\my documents\visual studio 2005\projects\ex6\ex6\ex6.c(42) : see declaration of 'f'
    Code:
    int smallest(char* f[a],char* f[b],char* f[c])
    {
         int i=1;
    	 char* pLowest = f[a];
         if(strcmp(pLowest, f[b]) > 0)
    		 i=2;
          pLowest = f[b];
          if(strcmp(pLowest, f[c]) > 0)
           pLowest = f[c];
    	  i=3;
          return i;
    }
    but i get *errors*
    you get errors
    because "[" and "]" is not a letter,number or underscore(_)
    that means what you gave is no longer a variable
    Last edited by creeping death; 03-29-2009 at 07:54 PM.
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  2. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  3. cannot print out my 2d array correctly! please help
    By dalearyous in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2006, 02:07 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM

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