-1

The title describes what I'm trying to do, but I'm getting the error message that I never declared base1. I actually know this, but I'm not exactly sure how to actually fix the problem.

int getBase1(void); int setBase1(double); int main(void){ getBase1(); setBase1(base1); } int getBase1(void){ printf("Please enter the length of a base: "); return; } int setBase1(double base1){ scanf("%lf", &base1); } 
11
  • 3
    The compile is telling you that base1 name is not defined inside the scope of main, which is entirely correct. You probably want to elaborate more on what you want to achieve. Commented Sep 19, 2017 at 13:25
  • Why not pass a reference for the variable? scanf will set value for that reference. Commented Sep 19, 2017 at 13:26
  • 3
    @DivyaMamgai there are no references in C. Commented Sep 19, 2017 at 13:26
  • because base1 is not defined Commented Sep 19, 2017 at 13:28
  • 1
    @DivyaMamgai if you mean "pointer" then you should write "pointer". And in C you cannot pass variables by reference as in C++. Commented Sep 19, 2017 at 13:38

3 Answers 3

6

You must use pointer, otherwise the variable inside the method will not point to the same memory adress. Using pointer you'll be putting the value inside the memory address of the variable that you pass in the function call. One more thing, this way you will not need return values.

Try this way:

#include <stdio.h> void getBase1(void); void setBase1(double *base1); int main(void){ double base1; getBase1(); setBase1(&base1); printf("%lf", base1); } void getBase1(void){ printf("Please enter the length of a base: "); } void setBase1(double *base1){ scanf("%lf", base1); } 
Sign up to request clarification or add additional context in comments.

13 Comments

The function prototypes in this answer still say that these functions should return int values. Nitpicks: for robust code, the return value from scanf() should be checked; and while %lf is not wrong for printing double values with printf(), the l modifier is not needed, and has no effect on the f conversion specifier (but it is needed with scanf()).
@DavidBowling My bad man. Thanks for your atention. Corrected!
printf("%lf", base1); -> printf("%f\n", base1);
@LethalProgrammer-- main() is a special case. No explicit return statement is needed here (since C99); in this case, a value of 0 is returned if the end of main() is reached without an explicit return.
@GabrielMagri-- int main(void) was fine, as this is one of the two function signatures for main() which is sanctioned by the Standard. void main(void) is an implementation-dependent signature, which is not portable.
|
1

Seems like you're quite new to C programming. Here's a thing, you simply can't use scanf to modify a value of a main function variable without using pointers. If you read about scanf, you would find out that scanf requires the memory address of a variable; that's why scanf is able to read the format string and modify your variable. So what you're trying to achieve is pretty much similar to scanf, you have to pass the address of base1; first of all declare it! Since that's what compiler is crying about. Do the following things:

  1. Declare and pass the address of the variable you want to modify. Pass the address of base1 like this:

    double base1; getBase1(); setBase1(&base1);

  2. In the function getBase1 you're doing a void return, but your function signature tells the compiler that you would return an int. So your functions should look like this:

    void getBase1(void); void setBase1(double *);

  3. Since your setBase1 is receiving an address, there is no need for an ampersand(&). Simply pass the pointer value received:

void setBase1(double *pBase) { scanf("%lf", pBase); }

Comments

0

You have many errors in your code, first int main(), should have the return type and your function doesn't return anything either. base1 is not declared.

error: ‘base1’ undeclared (first use in this function) setBase1(base1); ^ 

where is the base1 in your main function? Its basic your passing base1 as an argument to setBase1 but base1 is not declared.

1 Comment

@DavidBowling, I agree. I just spotted other errors as well. I will edit my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.