Thread: Passing Arrays into functions

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    60

    Passing Arrays into functions

    I am trying to write a program that will remove any double letters from a surname, ie if I entered harris it would return haris.

    I am trying to pass an array which is entered into the main program to the function rmdoub, firstly I get an error in the compiler telling me, incomparable types in assignment on the line where I try to pass the function the array. I have commented on this line to show it.\

    I'm not sure what is wrong with the syntax but the program doesn't work and I would really appreciate some help.

    Thanks guys


    Code:
    #include <stdio.h>
    
    char rmdoub(char surname[], int n)
    {
        int num;
        int i = 0;
        int j;
        int k;
    
        while (i < num){
              j = i + 1;
              printf("%d", j);
              printf("%d", i);
              if (surname[j] = surname[i]){
              k = i;
              printf("%d", k);
              printf("%d", num);
               
              num = num - 1;
                  while (k < num){
                       surname[k] = surname[j];
                       printf("%s", "c");
                       printf("%s", surname);
                       k = k + 1;
                       j = j + 1;
                  }
              }
              num = num + 1;
              i = i + 1;
                 
         }
         printf("%s", surname);
    
    }
    
    int main ()
    {    
         char surname[40];
         int num;
         char rmsurname[40];
         printf("Please enter the surname you wish to encode into the SOUNDEX index\n");
         scanf("%40s", surname);     
         num = strlen(surname);
         surname[num] = '\0';
         printf("%d", num);
         printf("%s", surname);
         surname = rmdoub(surname, 40); *// This is the line that I am getting an error on//
              
    }
    Last edited by Salem; 11-13-2006 at 06:15 AM. Reason: Changed colour to something readable

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    *blinded by unreadable yellow comment* Anyways, what doesn't work? Are there compilation errors or does the program not work as expected?
    EDIT:rmdoub returns a char. You need to specify a location in surname, such as surname[1]. Also, rmsurname is unused.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    yeh i forgot to delete the rmsurname bit, sorry about that just ignore it.

    I need the function rmdoub to pass back the array, as it changes the structure of the array. If this is at all possible.

    the program compiles but with the "incomparable types in assignment" error on the line with the unreadable yellow comment.

    Then the function doesn't work as I expected but that is a seperate problem, i'd rather sort the compiling problem first.

    Thanks

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Do what I said, use surname[1].
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    ok thanks, it compiles now.

    but I seem to have a problem with the IF statement, even if the characters stored in the ith and jth bit of the array surname are different the IF statement will carry on.

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    That's because you assign:
    Code:
    if (surname[j] = surname[i])
    This sets j to i. It should be:
    Code:
    if (surname[j] == surname[i])
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    oh damn I feel very stupid now.

    Thanks for your help it works perfectly now.

  8. #8
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Ah, two in a night...feelin' good
    p.s. Nice name
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    I am now using the following IF statement to check if a character is a vowel or not, although it doesn't seem to work.

    It should only perform the next operation if the letter equals a,e,i,o,u however it carries on regardless of the character. I am not entirely sure on whether the syntax is correct, I tried using "" around the characters but this gave me an error in compiling about comparing a pointer to an integer.

    i is a variable which is incremented by a while statement. I can post the whole piece of code if it is needed.

    Thanks in advance

    Code:
    if (surname[i] == 'a' || 'e' || 'i' || 'o' || 'u')

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    sorry ignore that question, it was another problem in the program that was causing it.

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    I tried using "" around the characters but this gave me an error in compiling about comparing a pointer to an integer.
    "" is for strings. '' is for individual characters. They are represented differently internally and cannot be replaced with each other in code, hence the error.



    Code:
    if (surname[i] == 'a' || 'e' || 'i' || 'o' || 'u')
    No, you need to test each one individually.

    Code:
    if(x == 'a' || x == 'b' //etc...
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    thanks, it works properly now.

    is it possible to enter both letters and numbers into an array, because if defined as either char or int I would think this wouldn't work as they are different data types. How would I go about defining an array so it can contain both types of characters.

  13. #13
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    It depends. The number 9 can be the character '9', if entered as such. If it is already defined as an int, use a function such as itoa.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    well I have defined it originally as char, because I needed to enter the surnames.

    But now I have to enter 3 letters into the array aswell, I am writing a program to convert into the soundex code.

    But when I try printing the array as it is currently I get symbols instead of any numbers.

  15. #15
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    thanks for all your help, I have got it to print out the numbers now.

    If you could just check whether the code below is correct or not I would be very grateful. I need it to enter the value 0 into the 2nd value in the array if there is not any of the numbers 0 - 6 currently stored in the array.

    Code:
    if (surname[1] != 1 || surname[1] != 2 || surname[1] != 3 || surname[1] != 4 || surname[1] != 5 || surname[1] != 6 || surname[1] != 0){
            surname[1] = '0';
            }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-03-2008, 11:31 AM
  2. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  3. Functions returning char arrays
    By turmoil in forum C Programming
    Replies: 3
    Last Post: 05-27-2003, 01:43 AM
  4. passing arrays through functions
    By Ecko in forum C++ Programming
    Replies: 4
    Last Post: 04-08-2003, 08:21 PM
  5. Passing multidimensional arrays to functions
    By maxthecat in forum C Programming
    Replies: 3
    Last Post: 12-22-2001, 03:58 PM