Click here to Skip to main content
15,030,448 members

Comments by Mieczyslaw1683 (Top 25 by date)

Mieczyslaw1683 15hrs ago 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 15hrs ago 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 15hrs ago View
   
Okay, I´ll try this way.
Mieczyslaw1683 15hrs ago View
   
Deleted
Okay, I´ll try to not use globals.
Mieczyslaw1683 15hrs ago View
   
Aha, okey.
Mieczyslaw1683 6-Sep-21 6:29am View
   
Aha, Interesting. One thing that is weird is that it feels like you are mixing the parameters of function "strcpy()". The right array goes to the left array. So I think it is like this:
strcpy(dest, source);
when it is more logical.
Mieczyslaw1683 19-Aug-21 10:17am View
   
Okey. Now I have seen the wrong code and fixed it (should be loop/loop2 instead of arrayc[loop]/arrayc2[loop2]). I will learn some more about debbuging also. Thank you for the help.
Mieczyslaw1683 6-Aug-21 12:23pm View
   
Okay. So in pointer arithmetic when subtracting two pointers (to elements in an array), then you need to use address-pointers (they have a "&" symbol before them). I tried this code below:
// Jones, Bradley L.; Peter Aitken; Dean Miller. C Programming in One Hour a Day, Sams Teach Yourself (pp. 339-340). Pearson Education. Kindle Edition.
   /* ptr_math.c--Demonstrates using pointer arithmetic to
      access array elements with pointer notation. */

   #include <stdio.h>
   #define MAX 10

   // Declare and initialize an integer array.

   int i_array[MAX] = { 0,1,5,3,4,5,6,7,8,9 };

  // Declare a pointer to int and an int variable.

  int *i_ptr, *i2_ptr, *i3_ptr, count;

  // Declare and initialize a float array.

  float f_array[MAX] = { .0, .1, .2, .3, .4, .5, .6, .7, .8, .9 };

  // Declare a pointer to float.

  float *f_ptr, *f2_ptr, *f3_ptr;

  int main( void )
  {
      /* Initialize the pointers. */

      i_ptr = i_array;
      i2_ptr = i_array[2];
      i3_ptr = i_array[3];
      f_ptr = f_array;
      f2_ptr = &f_array[2];
      f3_ptr = &f_array[3];

      /* Print the array elements. */

    //  for (count = 0; count < MAX; count++)
    //      printf("%d\t%f\n", *i_ptr++, *f_ptr++);
    for (count = 0; count < MAX; count++)
    {
    printf("%d\t%f\n", *i_ptr, *f_ptr );
    i_ptr++;
    f_ptr++;
    }
    printf("\n%d\n", i2_ptr - i3_ptr );
    printf("\n%d\n", f2_ptr - f3_ptr );

      return 0;
  }


To try to see if it would give me the value "2" from the pointer arithmetic subtraction: Copy Code
printf("\n%d\n", i2_ptr - i3_ptr );
. It gave me a "0". This made me wonder a bit. It looks wrong to be doing pointer arithmetic with pointers that have values.

However the correct way is to specify that its adress-pointers and then do the pointer arithmetic.
Mieczyslaw1683 6-Aug-21 11:51am View
   
Ok, thanks. It seems logical to use float pointers to float arrays.
Mieczyslaw1683 6-Aug-21 10:59am View
   
Ok. So yes. I have noticed that I skipped the "*" symbol before so they became float values. Because I got this error when making pointers to the f_array elements: "... \Giraffe\main.c|34|error: incompatible types when assigning to type 'float *' from type 'float'|
... \Giraffe\main.c|35|error: incompatible types when assigning to type 'float *' from type 'float'|"

Why do I get that errors when making pointers to the array elements?
Mieczyslaw1683 6-Aug-21 10:56am View
   
Hmm, I don't understand. The result is weird with that & symbol before. However, this is what I understand how the code should look and work correctly:
// Jones, Bradley L.; Peter Aitken; Dean Miller. C Programming in One Hour a Day, Sams Teach Yourself (pp. 339-340). Pearson Education. Kindle Edition.
   /* ptr_math.c--Demonstrates using pointer arithmetic to
      access array elements with pointer notation. */

   #include <stdio.h>
   #define MAX 10

   // Declare and initialize an integer array.

   int i_array[MAX] = { 0,1,2,3,4,5,6,7,8,9 };

  // Declare a pointer to int and an int variable.

  int *i_ptr, *i2_ptr, *i3_ptr, count;

  // Declare and initialize a float array.

  float f_array[MAX] = { .0, .1, .2, .3, .4, .5, .6, .7, .8, .9 };

  // Declare a pointer to float.

  float *f_ptr, *f2_ptr, *f3_ptr;

  int main( void )
  {
      /* Initialize the pointers. */

      i_ptr = i_array;
      i2_ptr = i_array[2];
      i3_ptr = i_array[3];
      f_ptr = f_array;
      f2_ptr = f_array[2];
      f3_ptr = f_array[3];

      /* Print the array elements. */

    //  for (count = 0; count < MAX; count++)
    //      printf("%d\t%f\n", *i_ptr++, *f_ptr++);
    for (count = 0; count < MAX; count++)
    {
    printf("%d\t%f\n", *i_ptr, *f_ptr );
    i_ptr++;
    f_ptr++;
    }
    printf("\n%d\n", i2_ptr - i3_ptr );
    printf("\n%f\n", f2_ptr - f3_ptr );

      return 0;
  }

The f2_ptr and f3_ptr are real pointers but the error when trying this code is:
"... \Giraffe\main.c|34|error: incompatible types when assigning to type 'float *' from type 'float'|
... \Giraffe\main.c|35|error: incompatible types when assigning to type 'float *' from type 'float'|"
Mieczyslaw1683 3-Aug-21 15:55pm View
   
Ok. So maybe the PC has an invisible indexer for pointer-incrementation which it uses but we cant see it?
Mieczyslaw1683 3-Aug-21 15:49pm View
   
Ok. Looks like you took your time, thanks. I think that the example before the last one looks most easy for me (incrementing the pointer after displaying the values).

Now when the first increment takes place (i_ptr++;), is the array altered in a way so that it now has a number added telling about the second item in the array?
Mieczyslaw1683 3-Aug-21 15:37pm View
   
Hmm. Ok. So the first time in the loop its just the item 0 (in i_array). Then the second time it is item 1. Then the third time it should be item 1 again (1) because its still just *i_ptr++ according to my logic? Where does the pc add the numbers?
Mieczyslaw1683 6-Jun-21 4:11am View
   
Okey, now the program works as intended (with a & before). Weird that this site: https://www.tutorialspoint.com/c_standard_library/c_function_scanf.htm Is showing without the "&" sign before the char that the value should go into. Thanks for the reply.
Mieczyslaw1683 30-May-21 10:15am View
   
Okay. So I have checked recursion on Wikipedia. Then I have checked the Debugger in CodeBlocks (I am using CodeBlocks). I understand how "Watches" works where I can see the variable numbers. I also understand the "Run to cursor" and "Next line" and "Step into" buttons now.

However is there maybe something I missed in the debugger because I dont see any multiplications?
Mieczyslaw1683 20-May-21 5:56am View
   
CodeBlocks.
Mieczyslaw1683 20-May-21 5:03am View
   
It (the debugger) is completly empty.
Mieczyslaw1683 1-Jan-21 14:41pm View
   
Okey. Thanks for the answer. I like good books.
Mieczyslaw1683 30-Dec-20 20:13pm View
   
How do you learn C programming language if not by YouTube lessons?
Mieczyslaw1683 30-Dec-20 20:13pm View
   
Thank you, this code works as intended.
Mieczyslaw1683 29-Dec-20 17:21pm View
   
Okey. Yes, it was a mistake to miss the main function. So now the code looks like this:
#include <stdio.h>
#include <stdlib.h>

/* https://www.mikedane.com/programming-languages/c/building-a-guessing-game/ */

int main() {

int secretNum = 7;
int guess;
int guessCount = 0;
int guessLimit = 3;
int outOfGuesses = 0;
    while (guess != secretNum && outOfGuesses == 0){
        if (guessCount < guessLimit){
            printf("Enter a guess: ");
            scanf("d", &guess);
            guessCount++;
     } else {
          outOfGuesses = 1;
     }
    }

    if (outOfGuesses != 0){
     printf("You Lose!");
    } else {
     printf("You Win!");
    }

}


It is not working as intended. Can someone please help to fix it?
Mieczyslaw1683 25-Dec-20 18:10pm View
   
Okey. Thank you for the answer.
Mieczyslaw1683 25-Dec-20 17:30pm View
   
I wonder one thing. When it comes to line 11.
Line 11: if ( scanf("%s", yesman) == 1)

I understand that scanf is scanning for input which goes to the yesman variable. What I don't understand is what the equal 1 (== 1) does? You could maybe spell out the meaning of the code to me in this line?
Mieczyslaw1683 20-Dec-20 18:23pm View
   
This code made the calculator to work correctly:
#include <stdio.h>
#include <stdlib.h>

int main(){

    float num1, num2;
    char op;

    printf("Enter num1: ");
    scanf("%f", &num1);

    getchar();

    printf("Enter Operator: ");
    scanf("%c", &op);

    printf("Enter num2: ");
    scanf("%f", &num2);

    /* printf("%f\n", num1); */

    if(op == '+'){
        printf("%f", num1 + num2);
    } else if(op == '-'){
     printf("%f", num1 - num2);
    } else if(op == '/'){
     printf("%f", num1 / num2);
    } else if(op == '*'){
     printf("%f", num1 * num2);
    } else {
     printf("Invalid Operator");
    }

    return 0;
}


Thank you Rick York for learning me to change that %d to %f which made me use float and with float the calculator works. I still need that getchar line because when I press Enter I get like a double input.