Click here to Skip to main content
15,791,806 members

Comments by Mieczyslaw1683 (Top 26 by date)

Mieczyslaw1683 9-Oct-21 9:05am View    
Thank you for the answer.
Mieczyslaw1683 17-Sep-21 10:02am View    
So I looked here: https://www.geeksforgeeks.org/how-to-return-a-pointer-from-a-function-in-c/

What I found was that I could use a pointer function. So I made my function a pointer function
char* rec2stringsCPointrLongest(char Ac[], char A2c[]);
instead of
char rec2stringsCPointrLongest(char Ac[], char A2c[]);
.

The program as it is now and I think it works correctly now:
// Jones, Bradley L.; Peter Aitken; Dean Miller. C Programming in One Hour a Day, Sams Teach Yourself (p. 400). Pearson Education. Kindle Edition.
//
// Exercise 6, lesson 10.
// 6. Write a function that accepts two strings.
// Count the number of characters in each, and
// return a pointer to the longer string.

  #include <stdio.h>

  char A[18];
  char A2[18];

  char* rec2stringsCPointrLongest(char Ac[], char A2c[]);

  int main( void )
  {

     printf("\nEnter the string: ");
     gets(A);
     printf("\nEnter the string number 2: ");
     gets(A2);

     printf("\n%p\n", rec2stringsCPointrLongest(A, A2));

     return 0;
 }

char* rec2stringsCPointrLongest(char Ac[], char A2c[])
{

    int i = 0;
    int i2 = 0;

    while(Ac[i]!='\0') {
    i++;
    }

    while(A2c[i2]!='\0') {
    i2++;
    }

    printf("\nLength of [ %s ] is [ %d ]\n", Ac, i);
    printf("\nLength of [ %s ] is [ %d ]\n", A2c, i2);

    if(i > i2) {
        char *p_Ac;
        p_Ac = &Ac;
        printf("\nLength of the longest string ([ %s ]) is: [ %d ]. Returning pointer to the longest string (%p).\n", Ac, i, p_Ac);
        return p_Ac;
    }

    if(i < i2) {
        char *p_A2c;
        p_A2c = &A2c;
        printf("\nLength of the longest string ([ %s ]) is: [ %d ]. Returning pointer to the longest string (%p).\n", A2c, i2, p_A2c);
        return p_A2c;
    }

    if(i == i2) {
        printf("\nLengths of both strings ([ %s ] & [ %s ]) are the same: [ %d ] & [ %d ]. Returning pointer to first string.\n", Ac, A2c, i, i2);
        char *p_Ac;
        p_Ac = &Ac;
        return p_Ac;
    }

}


Now everything seems to work. :) Thanks for the explanations!
Mieczyslaw1683 17-Sep-21 9:48am View    
Okey. So I have read the input I have received here. This is my program now:

// Jones, Bradley L.; Peter Aitken; Dean Miller. C Programming in One Hour a Day, Sams Teach Yourself (p. 400). Pearson Education. Kindle Edition.
//
// Exercise 6, lesson 10.
// 6. Write a function that accepts two strings.
// Count the number of characters in each, and
// return a pointer to the longer string.

  #include <stdio.h>

  char A[18];
  char A2[18];

  char rec2stringsCPointrLongest(char Ac[], char A2c[]);

  int main( void )
  {

     printf("\nEnter the string: ");
     gets(A);
     printf("\nEnter the string number 2: ");
     gets(A2);

     printf("\n%p\n", rec2stringsCPointrLongest(A, A2));

     return 0;
 }

char rec2stringsCPointrLongest(char Ac[], char A2c[])
{

    int i = 0;
    int i2 = 0;

    while(Ac[i]!='\0') {
    i++;
    }

    while(A2c[i2]!='\0') {
    i2++;
    }

    printf("\nLength of [ %s ] is [ %d ]\n", Ac, i);
    printf("\nLength of [ %s ] is [ %d ]\n", A2c, i2);

    if(i > i2) {
        char *p_Ac;
        p_Ac = &Ac;
        printf("\nLength of the longest string ([ %s ]) is: [ %d ]. Returning pointer to the longest string (%p).\n", Ac, i, p_Ac);
        return p_Ac;
    }

    if(i < i2) {
        char *p_A2c;
        p_A2c = &A2c;
        printf("\nLength of the longest string ([ %s ]) is: [ %d ]. Returning pointer to the longest string (%p).\n", A2c, i2, p_A2c);
        return p_A2c;
    }

    if(i == i2) {
        printf("\nLengths of both strings ([ %s ] & [ %s ]) are the same: [ %d ] & [ %d ]. Returning pointer to first string.\n", Ac, A2c, i, i2);
        char *p_Ac;
        p_Ac = &Ac;
        return p_Ac;
    }

}


Why doesn't the return value come out correct in the main() but instead is wrong?
Mieczyslaw1683 17-Sep-21 9:44am View    
Okay, I´ll try this way.
Mieczyslaw1683 17-Sep-21 9:44am View    
Deleted
Okay, I´ll try to not use globals.