Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have coded a program as an exercise. In the program, the exercise question (6.) is written almost in the beginning.

My program is below:
// 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];
  int *p_A;
  int *p_A2;

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

  int main( void )
  {

     p_A = &A;
     p_A2 = &A2;

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

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

     return 0;
 }

int 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) {
        printf("\nLength of the longest string ([ %s ]) is: [ %d ]. Returning pointer to the longest string (%p).\n", Ac, i, *p_A);
        return *p_A;
    }

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

    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);
        return *p_A;
    }
*/
    //

        if(i > i2) {
        int *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) {
        int *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);
        return *p_A;
    }

}


What I don't know is if the returned pointer to the longest string should be a global pointer or a function-local pointer?

How do you think is the correct way to answer this question?

What I have tried:

I have tried to run the program using a global pointer and it worked. Now that part of the code is commented out and instead a function-local pointer is returned.
Posted
Updated 17-Sep-21 3:11am
v3
Comments
Richard MacCutchan 17-Sep-21 9:00am    
There is (almost) never a reason to use global variables. Also, why are you using int* types to point to character arrays? That will certainly lead to trouble. I am not sure what the rec2stringsCPointrLongest function is supposed to be doing, but again using int* on character arrays will give false results.
Mieczyslaw1683 17-Sep-21 9:42am    
Aha, okey.

Richard is right about the pointer type. A pointer is only a value to memory and so has no additonal intelligence to which it points. The rule is, that the pointer has the same type as the memory to which it points. Than a future reader and the current compiler are understanding the code. ;-)

I would use clearer names to improve the code.

C++
char firstText[18];
  char secondText[18];

char *firstPointer;
char *secondPointer;
 
Share this answer
 
Comments
Mieczyslaw1683 17-Sep-21 9:44am    
Okay, I´ll try this way.
If you have to ask, then you probably shouldn't use a global.

If there is a way to avoid it, then using one is the wrong solution, no matter how much easier it may seem.
If there isn't a way to avoid using it, then either you don't need it at all, or your design is probably wrong!

Put it like this: in the real world, I haven't used a single global variable in nearly two decades ... probably more!
 
Share this answer
 
Comments
Mieczyslaw1683 17-Sep-21 9:48am    
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 10:02am    
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!
Rick York 17-Sep-21 11:21am    
You are using two global variables : A and A2. Those do not need to be global. They can and should be local to your main function.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900