Thread: Call by reference for strings

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    36

    Call by reference for strings

    Write a program that consists of a main and two additional functions. The first additional function is used to read two sets of characters in two-dimensional field characters (maximum of most 50) Another additional function receives two strings (previously loaded in the first function) and returns:


    1 - if the first string has more capital letters of the English alphabet from another
    0 - if the first string has an equal number of capital English letters as the other
    -1 - If the first string has less English capital letters from the other



    Code:
    #include <stdio.h>
    #define MAX 50
    
    
    void stringInput( char *pstring1[] , char *pstring2[] ){
        
        printf("Enter the value of your strings: ");
        scanf("%s %s", pstring1 , pstring2 );
        
        return;
    }
    char stringComparison( char string1[], char string2[]){
        
        int i, counter1 = 0 , counter2 = 0;
        for ( i = 0; string1[i] != '\0'; i++){
            if( string1[i] >= 'A' && string2[i] <= 'B'){
                ++counter1;
            } 
        }
        for ( i = 0; string1[i] != '\0'; i++){
            if( string1[i] >= 'A' && string2[i] <= 'B'){
                ++counter2;
            } 
        if( counter1 > counter2){
            return 1;
        }else if( counter1 == counter2){
            return 0;
        }else{
            return -1;
        }    
    }
    int main(){
        
        char origString1 , origString2;
        stringInput(&origString1, &origString2);
        printf("\nYour strings after running the function\n: string 1 = %d , string 2 = %d", origString1 , origString2);
        /*int count = 10;
        int *int_pointer;*/
        stringComparison(origString1, origString2);
        return 0;
    }
    My question is how do I change values of my strings using function void stringInput();
    This syntax with using pointers seems not to work changing value of a string array. Must be happening because string array is a pointer by itself ?
    Last edited by ZeroesAndOnes; 05-31-2016 at 10:28 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > char origString1 , origString2;
    Start by making these arrays of char, then fixing what the compiler complains about.
    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.

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    36
    Quote Originally Posted by Salem View Post
    > char origString1 , origString2;
    Start by making these arrays of char, then fixing what the compiler complains about.
    Code:
    #include <stdio.h>
    #define MAX 50
    
    
    
    
    void stringInput( char *pstring1[] , char *pstring2[] ){
        
        printf("Enter the value of your strings: ");
        scanf("%s %s", pstring1 , pstring2 );
        
        return;
    }
    char stringComparison( char string1[], char string2[]){
        
        int i, counter1 = 0 , counter2 = 0;
        for ( i = 0; string1[i] != '\0'; i++){
            if( string1[i] >= 'A' && string2[i] <= 'B'){
                ++counter1;
            } 
        }
        for ( i = 0; string1[i] != '\0'; i++){
            if( string1[i] >= 'A' && string2[i] <= 'B'){
                ++counter2;
            } 
        }
        if( counter1 > counter2){
            return 1;
        }else if( counter1 == counter2){
            return 0;
        }else{
            return -1;
        }       
    }
    int main(){
        
        char origString1[MAX] , origString2[MAX];
        stringInput(&origString1, &origString2);
        printf("\nYour strings after running the function\n: string 1 = %d , string 2 = %d", origString1 , origString2);
        stringComparison(origString1, origString2);
        return 0;
    }
    Sorry I had exam today and I have a lack of sleep so my cognitive functions are minimum ,is this what you meant?
    (Feeling really dumb right now)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well it's a start, now compile with warnings to get
    Code:
    $ gcc -Wall foo.c
    foo.c: In function ‘stringInput’:
    foo.c:10:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char **’ [-Wformat]
    foo.c:10:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘char **’ [-Wformat]
    foo.c: In function ‘main’:
    foo.c:38:3: warning: passing argument 1 of ‘stringInput’ from incompatible pointer type [enabled by default]
    foo.c:7:6: note: expected ‘char **’ but argument is of type ‘char (*)[50]’
    foo.c:38:3: warning: passing argument 2 of ‘stringInput’ from incompatible pointer type [enabled by default]
    foo.c:7:6: note: expected ‘char **’ but argument is of type ‘char (*)[50]’
    foo.c:39:3: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat]
    foo.c:39:3: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘char *’ [-Wformat]
    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.

  5. #5
    Registered User
    Join Date
    Mar 2016
    Posts
    36
    Quote Originally Posted by Salem View Post
    Well it's a start, now compile with warnings to get
    Code:
    $ gcc -Wall foo.c
    foo.c: In function ‘stringInput’:
    foo.c:10:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char **’ [-Wformat]
    foo.c:10:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘char **’ [-Wformat]
    foo.c: In function ‘main’:
    foo.c:38:3: warning: passing argument 1 of ‘stringInput’ from incompatible pointer type [enabled by default]
    foo.c:7:6: note: expected ‘char **’ but argument is of type ‘char (*)[50]’
    foo.c:38:3: warning: passing argument 2 of ‘stringInput’ from incompatible pointer type [enabled by default]
    foo.c:7:6: note: expected ‘char **’ but argument is of type ‘char (*)[50]’
    foo.c:39:3: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat]
    foo.c:39:3: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘char *’ [-Wformat]
    I get
    error 38 43 C:\Users\Ivan\Documents\Untitled1.cpp [Error] cannot convert 'char (*)[50]' to 'char**' for argument '1' to 'void stringInput(char**, char**)'

  6. #6
    Registered User
    Join Date
    Mar 2016
    Posts
    36
    There is no way out I will figure out this by myself I started learning about pointers and functions two days ago.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes, so do I.
    Do you know what the fix is, based on the information you have at hand?
    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.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Try something like
    Code:
    void stringInput( char (*pstring1)[50] , char (*pstring2)[50] ){
         
        printf("Enter the value of your strings: ");
        scanf("%s %s", pstring1[0] , pstring2[0] );
    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.

  9. #9
    Registered User
    Join Date
    Mar 2016
    Posts
    36
    Quote Originally Posted by Salem View Post
    Try something like
    Code:
    void stringInput( char (*pstring1)[50] , char (*pstring2)[50] ){
         
        printf("Enter the value of your strings: ");
        scanf("%s %s", pstring1[0] , pstring2[0] );
    This works now thank you so much man you saved me another 2 hours of trying to google the answer.
    I was trying to google my answer but there was some explanation with malloc() and strlen() function also there was explanation using double pointers.

    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>


    Code:
    void stringInput( char (*pstring1)[50] , char (*pstring2)[50] ){
          
        printf("Enter your strings:\n");
        scanf("%s %s", pstring1[0] , pstring2[0] );
        return;
    }
    char stringComparison( char string1[], char string2[]){
         
        int i, counter1 = 0 , counter2 = 0;
        for ( i = 0; string1[i] != '\0'; i++){
            if( string1[i] >= 'A' && string2[i] <= 'B'){
                ++counter1;
            } 
        }
        for ( i = 0; string1[i] != '\0'; i++){
            if( string1[i] >= 'A' && string2[i] <= 'B'){
                ++counter2;
            } 
        }
        if( counter1 > counter2){
            return 1;
        }else if( counter1 == counter2){
            return 0;
        }else{
            return -1;
        }       
    }
    int main(){
         
        char origString1[MAX] , origString2[MAX];
        stringInput(&origString1, &origString2);
        /*printf("\nYour strings after running the function\n: string 1 = %s , string 2 = %s", origString1 , origString2);*/
        stringComparison(origString1, origString2);
        return 0;
    }
    But now I have another problem ,it seems like the function doesn't return correct values , it returns 0 no matter what strings I enter , is there a build in function to check how many capital letters are in our string , so I don't have to write a function by myself?
    Last edited by ZeroesAndOnes; 05-31-2016 at 01:48 PM.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Your comparison is utterly broken.
    Code:
    if( string1[i] >= 'A' && string2[i] <= 'B') {
        ++counter1;
    }
    This has two problems: 1) A and B are just two capital letters and you ignore all the others, and 2) I think you mean to find out if string1[i] or string2[i] lies somewhere in the range of A to Z, but you should count one string and then the other to do this right.

    The comparison written above, has the following bad effect.
    For simplicity sake, string1 is "AA" and string2 is "BB".
    Now we can substitute the variables in the code with values and step through the loop...
    Code:
    > if ('A' >= 'A' && 'B' <= 'B') {
    Short circuiting means that 'B' is never considered.
    A is more than or equal to A, so we proceed.
    >   ++counter1;
    > }
    Now counter1 is 1.
    
    > if ('A' >= 'A' && 'B' <= 'B') {
    This is the same comparison as before, so we have the same result.
    >   ++counter1;
    > }
    Now counter1 is 2.
    
    You can do the same thing for the next counter used, counter2. 
    The same strings are compared the same way.
    
    > if( 'A' >= 'A' && 'B' <= 'B'){
    >    ++counter2;
    >} 
    > if( 'A' >= 'A' && 'B' <= 'B'){
    >    ++counter2;
    >}
    Now, as long as you realize we've compared every character in the string, then of course counter1 == counter2 (2 == 2). There is no way it couldn't be. You just take the same exact steps twice.
    Last edited by whiteflags; 05-31-2016 at 02:58 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Call by Reference
    By njo in forum C Programming
    Replies: 7
    Last Post: 03-20-2016, 02:07 PM
  2. Replies: 6
    Last Post: 11-11-2013, 06:53 PM
  3. help!! call-by-reference and call-by-value
    By geoffr0 in forum C Programming
    Replies: 14
    Last Post: 04-01-2009, 07:15 PM
  4. Call-by-value Vs. Call-by-reference
    By Wiz_Nil in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2002, 09:06 AM
  5. call by reference and a call by value
    By IceCold in forum C Programming
    Replies: 4
    Last Post: 09-08-2001, 05:06 PM

Tags for this Thread