This question already has an answer here:
Variable changed in function not seen by caller?
(1 answer)
Closed 6 years ago.
My code is about implementing the function of Stack and Queue. If you're wondering why don't I use struct in my program that's because our instructor only allows us to use pointers and I'm a little bit confused using pointers. It's not yet finish, as much as possible I try to finish one function at a time instead of skipping to the next.
#include <stdio.h>
int container[5];
int len=0;
As I understand this part of my in function insert, "len" is suppose to change since it's incremented every time the function is use
int insert(int container[],int len)
{
int i,j,content,*incre;
incre=&len;
for(i=*incre;i<*incre+1;i++)
{
printf("Enter number:\n");
scanf("%d",&content);
container[i]=content;
}
(*incre)++;
}
int printcontent(int container[])
{
int i;
printf("Content of container:\n");
for(i=0;i<len+1;i++)
{
printf("%d ",container[i]);
}
printf("\n");
}
This part asks the user whether to add a content in the container or simply remove it. They can only add or remove one at a time.
int stack(int container[])
{
while(1)
{
int choice;
printf("What do you wanna do?\n");
printf("1.Insert\n2.Pop\n3.isEmpty\n4.Exit\n");
scanf("%d", &choice);
I checked the value of len it remains 0 instead of 1 when I first use insert.
if (choice==1)
{
insert(container,len);
printf("Value of len:%d\n",len);
printcontent(container);
}
else if(choice==4)
{
break;
}
}
}
main()
{
while(1)
{
int choice;
printf("What do you want to do?\n");
printf("1.Stack\n2.Queue\n3.Exit\n");
scanf("%d",&choice);
if(choice==1)
{.
stack(container);
}
else if(choice==3)
{
break;
}
}
}
Thanks for all of your help.
What your insert(...) function does is that it takes the parameter int len by value. That means if you call insert(...) with the local variable len as an argument in main(...) or whereever - it actually copies the value of the local variable len to the argument variable len inside the insert(...) function. The variable len which is inside the insert(...) function is then local to the insert function. That does not change even if you're declaring a pointer named incre pointing to this insert(...)-local variable len.
Then the argument variable len is incremented - but due to the fact that it is a local variable of the insert(...) function you will not see the original local variable len (which is in main(...) e.g.) being changed.
If you want to pass the original len by reference instead of by value, you have to declare your function like this ...
int insert(int container[], int* len) { ... } // Using raw pointers
... or like this ...
int insert(int container[], int& len) { ... } // Using references
... and use it like that (depending on the decision above):
insert(container, &len); // With pointers
insert(container, len); // With references
Related
I am creating a project and I encountered behavior that is not supposed to happen (at least as far as I know). I am trying to change values in a struct array inside of a function addOrder. in main() I give struct's address to a function addOrder (and this is the first warning). Then I try to change its contents in the named function. I should only be able to change using ->, however, I get errors this way and I can only change using data[0].someName
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define MENU_TITLE "----Welcome to order manager!---" // menu title
#define MENU_SIZE 4 //change based on menu options
#define INPUT_MSG "Please choose one of the following options" //enter input message (range is added later automatically)
#define SIZE 25 //size of option message array
#define STREET_NAME_SIZE 50 //size of street name array
#define BUILDING_NUMBER_SIZE 10 //size of building number array
#define EMAIL_SIZE 40 //size of email array
#define INPUT_PHONE_MAX 12
typedef struct Data{
char streetName[STREET_NAME_SIZE];
char buildingNumber[BUILDING_NUMBER_SIZE];
unsigned long long phoneNumber;
char email[EMAIL_SIZE];
}Data;
void loadOrderAmount(int *orderAmount);
void showMenu(char *menuTitle, int menuSize, char *inputMsg);
int getChoise();
void addOrder(Data *data, int *orderAmount);
void getEmail(char *email);
unsigned long long getPhoneNumber();
int main(){
int orderAmount = 0;
loadOrderAmount(&orderAmount);
Data data[orderAmount + 1];
int choise = 0;
showMenu(MENU_TITLE, MENU_SIZE, INPUT_MSG);
while(!choise){
choise = getChoise();
}
printf("\n");
switch(choise){
case 1:
addOrder(&data,&orderAmount); // This gives first warning
break;
case 2:
//removeOrder(data);
break;
case 3:
//editOrder(data);
break;
case 4:
printf("Have a nice day\n");
return 0;
}
printf("in main data %d\n",&data);
//printf("%s %s ",data[0].streetName,data[0].buildingNumber);
//printf("+%llu ",data[0].phoneNumber);
printf("in main %s %llu\n",data[0].email,data[0].phoneNumber);
return 0;
}
void addOrder(Data *data,int *orderAmount){
int amount = *orderAmount; //it is equal to 0
char email[41];
char street[51];
char buildingNumber[11];
printf("Adding an order...\n\n");
//++orderAmount;
// getStreetName(data, &orderAmount);
// getBuildingNumber(data,&orderAmount);
data[amount]->phoneNumber = getPhoneNumber(); //why this does not work?
getEmail(email);
strcpy(data[0].email,email); // why this works? its a function....
printf("In function data %d\n", data);
printf("in struct %s %llu\n",data[0].email, data[0].phoneNumber);
}
}
This is only a part of a code that is relevant and most functions are missing. Can anyone help me with this?
The function addOrder is declared like
void addOrder(Data *data, int *orderAmount);
But you are calling it passing the first argument of the type Data ( * )[orderAmount + 1]
addOrder(&data,&orderAmount);
You need to call it like
addOrder( data, &orderAmount );
Instead of this statement
data[amount]->phoneNumber = getPhoneNumber();
you have to write
data[amount].phoneNumber = getPhoneNumber();
And instead of this call
printf("In function data %d\n", data);
you have to write
printf("In function data %p\n", ( void * )data);
Pay attention to that within the function the expressions
data[amount]
and
data[0]
are not the same because amount is not equal to zero. The function is called with the argument orderAmount (passed by reference) that is assigned to amount.
This is how you solve this error:
Compiler points at the line addOrder(&data,&orderAmount); saying something strange "error passing argument 1 blablabla". We don't actually need to understand more than "error argument 1".
Could it be that the compiler is right and something is wrong with argument 1?
Hmm which one is argument 1, it must be &data.
Check the declaration of data, it is Data data[orderAmount + 1];. It's an array!
What did they teach us in beginner class? Ah, arrays decay to a pointer to the first element when passed to a function. So there is no need for &.
Change the call to addOrder(data,&orderAmount);
Compile again.
I'm trying to get the input from the user input, and pass to the program main using pointer. It seems that the value isn't going through even tho I tried some alternatives.
For example, if i input 0.0001, when I return to the main function it outputs 0.0000
#include <stdio.h>
void inputFloat(float *n);
int main() {
float *number;
printf("Insert number: ");
inputFloat(number);
printf("%1.4f", *number);
return 0;
}
void inputFloat(float *n){
float num;
while (scanf("%f", &num) != 1 || num < 0) {
scanf("%*[^\n]%*c");
printf("insert positive: ");
}
printf("%1.4f\n", num);
n = #
}
Thanks in advance for any tip or correction.
The main problem here is that you forget that in C arguments are passed by value, and that includes pointers.
The argument variable n in the inputFloat function is local to the inputFloat function, all modifications (like assignments) to it will be lost once the function returns.
What I believe you are trying to to is emulating pass by reference, which indeed is done by using pointers. But then you pass a pointer to a normal variable using the address-of operator &:
float number; // Not a pointer
inputFloat(&number); // Pass a pointer to the variable number
To use this pointer, and to set the value of the original variable it points to, you need to dereference the pointer:
void inputFloat(float *n)
{
// ...
*n = num; // Copy the value of num to the location where n is pointing
}
I'm having a headache here trying to solve this. The thing is, I'm trying to initialize a struct array by sending its address from the main() function to its specific function (in which initialize it). But when I run it, it just crashes all the time. But, the program goes until that flag over there! Please help me out
ps:
The error message says:
45 [Warning] passing argument 1 of 'initfunc' from incompatible pointer type [enabled by default]
typedef struct{
int code, year, rentstate;
char type[50];
float price;
}CAR;
void initfunc(CAR *car[]){
int i;
printf("flag ok");
for(i=0;i<50;i++)
{
car[i]->code = 0;
printf("initializing...");
}
getch();
}
int main(int argc, char const *argv[])
{
int opt, a=0;
CAR car[50];
initfunc(car);
CAR *car[]
means array of CAR pointers.
You want to send CAR car[] array of structure CAR.
Also as you pass the array of CAR you can access it like this car[i].code or car[i].year etc.
You would have to access code like it, if it was a CAR* not CAR.
For example:
CAR *car = malloc( sizeof *car);
...
(*car).code = //..
equivalently
car->code = //...
In your case the code would be
void initfunc(CAR car[]){ // void initfunc(CAR *car)
printf("flag ok");
for(size_t i = 0; i < 50; i++)
{
car[i].code = 0;
printf("initializing...");
}
getch();
}
OP asked why changing the array to the called function changes the array in main()?
Since we are actually passing pointer to the original array, change in the called function modifies the original array. In C, except for a few special cases, an array reference always "decays" to a pointer to the first element of the array.
In C everything is pass-by-value. So the pointer variable that we see in called function contains the copy of the address. Any changes made to the array elements (by de-referencing the pointer) do affect the original array, [since only the array address is copied (not the array elements themselves)].
Simply put, here you pass the address of the 0-th element of the array and then that is accessed. That's why the change is reflected because you are accessing the original elements via pointer, not the copies of them.
I just tried like this:
typedef struct{
int code, year, rentstate;
char type[50];
float price;
}CAR;
void initfunc(CAR car[]){
int i;
printf("flag ok");
for(i=0;i<50;i++)
{
car[i].code = 0;
printf("initializing...");
}
getch();
}
int main(int argc, char const *argv[])
{
int opt, a=0,i ;
CAR car[50];
for(i=0;i<50;i++){.
car[i].code = -1;
printf("%d", car[i].code);
}// RECEIVING -1 JUST TO TEST IF IT REALLY CHANGES AFTER I CALL THE FUNCTION TO INITIALIZE
printf("\n\n");
initfunc(car); //HERE WE'RE SUPPOSED TO INITIALIZE
printf("\n\n");
for(i=0;i<50;i++){
printf("%d", car[i].code);
}
printf("\n\n"); // OMG THIS CANT HAPPEN, WHY IS THIS EVEN HAPPENING? IM NOT EVEN USING POINTERS!
getch();
I am just starting to learn programming for a unit I am doing in my engineering course and I have come across pointers. I just wanted some reassurance that I actually understand the concept correctly in terms of using a pointer as an argument in a function. If I understand it correctly, you pass a pointer to an address of a variable you would like to be altered by a separate function called, even though it is a local variable within the scope of the calling function. Does that make sense? I have an example from my text book which I re-wrote. The only thing is they gave it in two incomplete parts and I put it together, filled in the blanks and added the final printf statement in the main function. I'll paste it here:
#include <stdio.h>
#include <stdlib.h>
#define READ_OK 0
#define READ_ERROR 1
int read_num(int lo, int hi, int *num);
int main(int argc, char *argv[])
{
int lo = 0, hi = 0, *num, val;
printf("Please enter a lower bound and an upper bound for your range,respectively\nLower: ");
scanf("%d", &lo);
printf("Upper: ");
scanf("%d", &hi);
num = &val;
if(read_num(lo,hi, &val) != READ_OK)
{
printf("Read error, program abort\n");
exit(EXIT_FAILURE);
}
else
{
printf("You entered %d, press any key to continue: \n", val);
getchar();
}
return 0;
}
int read_num(int lo, int hi, int *num)
{
int next;
printf("Enter a number between %d and %d: ", lo, hi);
while(scanf("%d", &next)==1)
{
if (lo<=next && next<=hi)
{
*num = next;
return READ_OK;
}
printf("%d is not between %d and %d\nTry again: ", next, lo, hi);
}
return READ_ERROR;
}
So is my understanding correct? "val" gets modified in read_num() by passing it's address in the form of pointer "*num", in which the the value for "next" is then written?
PS: is this syntax correct?
PPS: What would this process specifically be called?
Thanks a bunch for any help :)
The *num is not necessary inside the main() function. As you are passing the address of the val inside the read_num() , so any changes from the read_num() will also affect the value inside main() as you are working with the address.
In your program you have basically use two different pointers- one is inside main which is num, and another inside read_num() which is also num, for more understanding see the scope of a variable in c. As the val is inside main so you don't need to use pointer here, because you have the access of changing the value from the main as it is local to it. You will need pointer when you will be changing the value of val outside from the main, or from outside of the scope of the variable.
I have a simple function that I am trying to create. I need this function to add 6 to whatever number the user decides to enter into the program. I have been working with this code for an hour now and cannot figure out what I'm doing wrong, even after looking at multiple examples from the course I am taking. I really appreciate the help.
Here is my code:
#include <stdio.h>
void closing(void);
void addSix(void);
int x;
int result;
int main()
{
int x;
printf("Please enter a number to add to 6: ");
scanf("%d", &x);
getchar();
addSix();
closing();
closing();
return 0;
}
void closing(void)
{
printf("That's all folks.\n");
}
void addSix(void)
{
int result = x+6;
printf("Result: %d\n", result);
}
You've got both a global x variable outside of main(), and a local x inside of main(). The code inside of main() writes to the local x while the code in addSix() reads from the global x.
Remove the int x declaration in main() so that both places access the global x.
You have x declared as both a global variable and variable local to main. When you pass x by pointer to scanf, it refers to the local variable, not the global.
You can remove the local declaration of int x in main but this isn't really the best solution. Generally, global variables should be avoided when possible (though they are, of course, sometimes necessary/the best tool for the job).
The best solution in this case is to make x a parameter to addSix(). There are a few options here:
You can have addSix return the sum and then use that return value
You can pass the address of x to addSix and have the function modify x itself by using a pointer.
The former would look like this:
int addSix(int x) {
return x + 6;
}
The latter would look like this:
void addSix(int * x) {
*x += 6;
}
You have two variables named x. One is a local to main, and the other is a global variable.
The function addSix cannot see the local in main. It can only see the global variable.
You should change addSix so that it is passed the value as a parameter.
void addSix(int x)
{
printf("Result: %d\n", x+6);
}
Call the function like this:
addSix(x);
Or perhaps you want your function to return a value:
int addSix(int x)
{
return x+6;
}
Which you can call like this:
int result = addSix(x);
Both of your global variables are needless. Remove them.
You have a global x, and a local x variable in which you actually save the user input.
Using only the global variable will do the trick for you
#include <stdio.h>
int x;
void addSix(void)
{
int result = x+6;
printf("Result: %d\n", result);
}
int main()
{
printf("Please enter a number to add to 6: ");
scanf("%d", &x);
getchar();
addSix();
return 0;
}