-1

I am learning functions in C and having following problem with so many warnings :-(

code is as follows

#include <stdio.h>
void main(){
    int a,c;    
    char *b;    // declaring a pointer 
    char string[100]="something";
    b = string;     // assigning a pointer.. doing
    printf("\n %s \n\n %s",string,*b); // doing this as a verification of the pointer, which is good - no seg faults 
    printf("Enter a and c:-");  
    scanf("%d %d",&a,&c);
    find(a,c,*b);
    printf("%s",*b);//segmentation fault core dumped:-'(
}
void find(int x, int y,char  *b){
    if(x>y)
        *b = "a is greater than b";
    else if(x=y)
        *b = "both the values are equal";
    else
        *b = "b is greater than a";
}

warnings while compiling:--

function.c: In function ‘main’:
function.c:7:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat=]
  printf("\n %s \n\n %s",string,*b); /*doing this jus to check is pointer working but no it is           *not.segmentation error here "core dumped":-'( 
  ^
function.c:12:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
  printf("%s",*b);//segmentation fault core dumped:-'(
  ^
function.c: At top level:
function.c:14:6: warning: conflicting types for ‘find’ [enabled by default]
 void find(int x, int y,char  *b){
      ^
function.c:11:2: note: previous implicit declaration of ‘find’ was here
  find(a,c,*b);
  ^
function.c: In function ‘find’:
function.c:16:6: warning: assignment makes integer from pointer without a cast [enabled by default]
   *b = "a is greater than b";
      ^
function.c:18:6: warning: assignment makes integer from pointer without a cast [enabled by default]
   *b = "both the values are equal";
      ^
function.c:20:6: warning: assignment makes integer from pointer without a cast [enabled by default]
   *b = "b is greater than a";

while running


  • segmentation error (core dumped)

Working cod:=---- with the help of community

#include <stdio.h>
#include <malloc.h>
void main(){
    int a,c;    
    char *b =(char *)malloc(100);       
    char string[100]="something";
    b = &string;    
    printf("\n %s \n\n %s",string,b);
    printf("Enter a and c:-");  
    scanf("%d %d",&a,&c);
    find(a,c,*b);
    printf("\n%s",b);
}
void find(int x, int y,char  *b){
    if(x>y)
        b = "a is greater than b";
    else if(x=y)
        b = "both the values are equal";
    else
        b = "b is greater than a";
}

output:- something

something enter a and c:- 10

20

something

**

  • means still it is not updating the value in the function...

**

Cœur
  • 37,241
  • 25
  • 195
  • 267
Khushit Shah
  • 546
  • 6
  • 20
  • 2
    Start by fixing the warnings. In this case they are telling you very important things you have done wrong. – John3136 Mar 27 '18 at 03:17
  • @John3136 can't we ignore errors? – Khushit Shah Mar 27 '18 at 03:18
  • I'm a student learning C in ubuntu – Khushit Shah Mar 27 '18 at 03:18
  • 1
    Look where ignoring warnings got you :) Even enable more warnings `-Wall -Wextra -pedantic` and make sure there are no warnings when you compile. – Paul Rooney Mar 27 '18 at 03:19
  • in First warning String and *b are strings only why compiler are considering it as int – Khushit Shah Mar 27 '18 at 03:20
  • 1
    You *think* `*b` is a string. You think wrong. – John3136 Mar 27 '18 at 03:21
  • 1
    `printf("\n %s \n\n %s",string,*b);` => `printf("\n %p \n\n %s",string,b);`. `string` is a pointer, so should not be printed as a string. `b` is a char pointer, so if you dereference it you get `char`. – Paul Rooney Mar 27 '18 at 03:22
  • 2
    warnings are designed to point out issues which are technically legal with the compiler/linker, but have a high probability of causing you trouble during run time. You'd be amazed as to how many programmers just ignore them whenever they can. Pro tip - sanitize your code so it does not have any warnings. You'll thank me later ;) – Rann Lifshitz Mar 27 '18 at 03:22
  • @John3136 please see the code now it is working half way. – Khushit Shah Mar 27 '18 at 03:26
  • 1
    @J.JOE Please please please go back to your tutor / textbook / online course. You can't learn C by trying random things. Now as well as incorrect pointer types and code that probably doesn't do what you think it does, you've now got a memory leak. – John3136 Mar 27 '18 at 03:33
  • @John3136 okay. – Khushit Shah Mar 27 '18 at 03:36
  • You can't ignore warnings.The first step for professional code is a clean bill from warnings. – Tsakiroglou Fotis Mar 27 '18 at 09:09

1 Answers1

1

I fix your code in less than a minute.No warnings and probably works (I don't speaking for logical problems).This means that your warnings were common mistake that even an experienced engineer might do.However a smart engineer will use warnings to fix these warnings.

#include <stdio.h>
#include <malloc.h>

void find(int, int,char*);

int main(){
    int a,c;
    char *b = NULL;
    char string[100]="something";
    b = &string[0];
    printf("\n %s \n\n %s",string,b);
    printf("Enter a and c:-");
    scanf("%d %d",&a,&c);
    find(a,c,b);
    printf("\n%s",b);
    return 0;
}
void find(int x, int y,char  *b){
    if(x>y)
        b = "a is greater than b";
    else if(x == y)
        b = "both the values are equal";
    else
        b = "b is greater than a";
}

If you check this , you will see that there is no warning now. Regarding on your effort to learn , I would try to explain warnings and try to estimate and give you a big picture of what those warnings would result.

warning: return type of ‘main’ is not ‘int’

-Here you take the answers in why we prefer to return int in main. int main vs void main

warning: suggest parentheses around assignment used as truth value

-This is the worst error you have. = is for asign , == is for comparison Difference between = and ==

The other too errors ,

conflicting type

and

implicit declaration

means that preprocessor of your compiler reached function invocation ( find(a,c,*b); ) before the declaration of the function.So one compiler might fix that and auto resolve this while another compiler might have an error.That's why preprocessor check firstly the header file , but since you don't have a header file (that's bad) , you should have a declaration of the function before you invoke this. Whilst you fix that you would receive a warning ,

warning: passing argument 3 of ‘find’ makes pointer from integer without a cast

This means that you are trying to pass the pointer of the pointer. b was a pointer at the first place , which hold the address of the first element of your char array.So y should have pass this without *.

THIS is a very good example on the question Why we don't ignore error

.Because an error about implicit declaration of a function , led as to discover another error , more significant than the first , that was hiding under the first warning.

Another note is that you don't need to malloc there.You malloc when you want to allocate an amount of memory on the heap and keep it alive before you free it.

PS:I hope I help and I hope you also take some benefit out of these.If you have any question , do not hesitate to comment(better in chat , to avoid spam in the community)

Thank you.