DaidalosCheung / C

0 stars 0 forks source link

Troubleshooting #2

Open DaidalosCheung opened 4 years ago

DaidalosCheung commented 4 years ago

Incompatible pointer to integer conversion passing

incompatible pointer to integer conversion passing
      'char *' to parameter of type 'int' [-Wint-conversion]
    putchar( r == 1 ? "1" : "0" );
             ^~~~~~~~~~~~~~~~~~
/usr/include/stdio.h:524:25: note: passing argument to parameter '__c' here
extern int putchar (int __c);
                        ^
review.c:13:14: warning: incompatible pointer to integer conversion passing
      'char *' to parameter of type 'int' [-Wint-conversion]
    putchar( r == 1 ? "1" : "0" );
             ^~~~~~~~~~~~~~~~~~

/usr/include/stdio.h:524:25: note: passing argument to parameter 'c' here extern int putchar (int c);

The compiler treat "1" and "0" as string, but it should be use as a int. To pointer it is int** not **int***

DaidalosCheung commented 4 years ago

const vs static

What is the difference between const & static

using namespace std; 
// function to add constant value to input 
int addConst(int input) 
{ 
    // *** value = 5 will be **preserved** in memory
    // *** even after the execution of the function is finished 
    static const int value = 5; 

    // *** constant_not_static will not accept change in value but it will get 
    // **destroyed**_ after the execution of
    // function is complete 
    const int constant_not_static = 13; 
    input += value; 

    // value++; ==> this statement will produce error 
    return input; 
} 
DaidalosCheung commented 4 years ago

compare pointer and integer

warning: comparison between pointer and integer ('int' and 'char *')

#include <stdio.h>
#define LEN 10

int main(void){
  char words[LEN];

  printf("Please input your string: \n");
//while (fgets (words, LEN, stdin) != NULL && words[0] != "\n" ){
//_warning: comparison between pointer and integer ('int' and 'char *')_
// Because **"\n"** is a string, compiler treat it in a pointer to first element, but '\n' is a int (char)
  while (fgets (words, LEN, stdin) != NULL && words[0] != '\n'){
    fputs(words, stdout);}

  printf("\n");

  return 0;
}
DaidalosCheung commented 4 years ago

List of pointer and pointer of list

list[n] & (list)[n]

DaidalosCheung commented 4 years ago

static cannot alter its value

#include <stdio.h>
#include <stdlib.h>
static unsigned long seed; // static unnecessary, cause only one file
int random_int (unsigned long seed);

int main(void) {

  int seed_rand;

  printf("Input a seed to generate random number: \n");
  scanf("%d", (&seed_rand) );
  seed_rand = abs (seed_rand);

  seed = seed_rand; //seed still globle rand
  printf("\n%p\n", &seed);

  for (int i = 0; i < 5; i++)
    // both seed are globel seed, only call function will make a copy
    printf( "%lu, %d\n", seed, random_int (seed) );

  return 0;
}

// declearation & make a copy of local variable 
int random_int (unsigned long seed){
  seed = seed * 438195 + 5987249;
  printf("\n%p\n", &seed);
  return seed % 32768;
}

在这个程序里面,每次调用 “seed” 的时候会发现它的值并没有随着“ seed = seed * 438195 + 5987249; ” 改变,但是如果将 function “int random_int (unsigned long seed)” 里面接收的变量删除掉,seed 就可以在每次被调用的时候被赋予新值,不知道这是为什么?