C :: Functions To Prove Variables Not Changing By Another Function

Jul 8, 2013

It is said that variables in a function cannot be changed by another function. Only by using pointers can variable values be changed. I am writing some functions to try to prove this theory, but I can't get it right.

Code:

#include <stdio.h>
#include <stdlib.h>
int main(void){
int x = 10;
printf("default x value is %d ",x);

[Code] ......

Code:

#include <stdio.h>
void try1(int x){
printf("x in try1 is %d
", x);
x++;
printf("x in try1 after ++ is %d

[Code]...

View 13 Replies


ADVERTISEMENT

C/C++ :: Main Function Can Only Declare Variables And Functions

Mar 9, 2015

I need making my main function to run while not having any if or for statements. It can only declare variables and functions. Since my main function has command line arguments, how to so.

// This program counts all the words in a given file.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[]) {
FILE* txtFile = NULL; // File Pointer
char str[1000000];

[Code] ....

View 5 Replies View Related

C :: How To Make Main Function Only Allowed To Declare Variables And Functions

Mar 6, 2015

So I need to make a main function have no if/for/etc. statements so I need to move it to another function and call it in main. The problem is that it's a command line argument function so I'm confused on how it works. Here's an example:

Code:

#include <stdio.h>
int main(int argc, char* argv[])
{
printf("The program name %s", argv[0]);
if (argc == 2) {
printf("Argument supplied is %s", argv[1]); }
else if (argc > 2) {
printf("Too many arguments");}
else {
printf("One argument");}
}

How can i make this into two functions with main only declaring variables and calling other functions?

View 2 Replies View Related

C++ :: Changing Variables In Other Classes Code Blocks?

May 20, 2013

I want to make a basic RPG text based games with multiple classes. but I wan to know how In one class I make a variable and in another and can call the variable.

for exmaple

class2:
int hi = 1;
class1:
class2 a;
hi = 2;
cout << hi;
2

View 1 Replies View Related

C++ :: Prove Random Generated Number Is Uniform Distributed?

Aug 20, 2014

Given a random number generator to generate a number between [0,N), how to prove this number is uniform distributed.

View 1 Replies View Related

C/C++ :: Use The Same Variables Between Functions?

Mar 6, 2015

I'm trying to use two variables for two different functions.

#include "stdafx.h"
#include <iostream>
#include <cstdlib>

[Code].....

Every time screen2() runs it always outputs "Numbers entered were 0 and 0". I want the numbers that were entered in by the user in screen1() to be displayed.

View 2 Replies View Related

C :: Passing Variables Among Functions

Mar 6, 2015

I have this code where I am trying to retrieve the contents of the variable dev1 and dev2. for some reason when i compile and run I am getting 0 and 0.

Code:

typedef struct {
uint32_t x;
uint32_t y;
} sample;
void get_sample(sample *one)

[Code].....

View 9 Replies View Related

C :: Pass Variables Between Functions

Jun 29, 2014

I'm playing around with GTK+ 2.x and currently I have a button to call a function like so:

Code:
void selectmod(GtkWidget *downloadbutton, GtkComboBox *modlist, gchar *data){
gchar *mod = gtk_combo_box_get_active_text(modlist);
printf("
%s", mod);
}

How can I pass the *mod variable to another function?

View 9 Replies View Related

C# :: Arrays In Functions And Variables

May 21, 2014

I'm truing to write a function that gets an array of arrays, and returns an array. I placed the code I'm using below, but I'm getting all kinds of errors. How is the right way to do this?

static void Main(string[] args) {
int[] draw1 = new int[] { 5, 8, 3 };
int[] draw2 = new int[] { 3, 6, 8 };
int[] draw3 = new int[] { 6, 7, 9 };
int[] draw4 = new int[] { 5, 6, 0 };

[Code] .....

View 4 Replies View Related

C/C++ :: Variables From Different Functions Don't Match

Dec 4, 2014

In the code below there is some error, I think it is related with functions. It compiles properly but then it outputs only : "humanslife8.90145e+032"

#include <iostream>
#include <windows.h>
#include <stdlib.h>
using namespace std;
void userInput(float HumanNumber, float SkeletonNumber, float AllHumanslife, float AllSkeltonslife, float SkeletonHealth, float HumanHealth);
void HumansTurn(int turn, float HumanAttack, float AllSkeletonslife, float HumanNumber, float HumanDamage, float SkeletonHealth, float SkeletonNumber);

[code].....

View 3 Replies View Related

C++ :: Passing Variables Between Functions

Jan 29, 2013

I need passing some variables between functions. Here is the first part of the code which does work.

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct CarType {
string maker;

[Code] ....

Now the book says to take the following program and add a member function to the CarType class which prints the values of all of its data members. Add two more data members which are relevant for cars. Add the use of these data members to the program (to the assignment statements for MyCar, to the operator prompt and input inside the getYourCar function, and to the print function you have created).

Here is my code. Whenever I run it, it takes my assigned variables in MyCar and prints those instead of the one which the user is inputting.

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
using namespace std;
struct CarType {
string maker;

[Code] .....

View 3 Replies View Related

C/C++ :: How To Pass Structure Variables Between Functions

Jul 13, 2014

I have a project for class where I have to create a structure and get user input for 3 structure variable arrays of 10. I am trying to figure out how I can use the same function to fill my different section of variables.

My Structure is an employee file of ID number, name, hours, payrate, and then gross pay. I have to create a function for each input function. I am confused on how to pass the structure variable so that I do not have to write 3 functions for each input. I would like to be able to get all the info for the first structure variable and then recall the same 5 functions for the next before moving along. I hope that I have been able to make this clear. Here is my code:

#include<iostream>
#include<iomanip>
#include<cctype>
#include<string>
#include<cstring>
using namespace std;
struct PayPeeps_CL//Payroll Structure {

[Code] .....

View 14 Replies View Related

C++ :: Passing Both Private And Public Variables-functions?

Mar 6, 2015

How to properly pass both private and public variables/functions from one class to another?

View 3 Replies View Related

C :: Calling Functions With Arguments Using Pointer Variables As Operators

Feb 2, 2013

There are, or course, better ways to do this, but I need to stick to some rules:

(1) Use only pointer variables and not arrays or structs.
(2) Use the three functions shown--regardless of easier methods.

The program should ask for some input, operate on those numbers, and then display the results. I know I am confused over these things:

(1) All that syntax using '*' and '&' or neither.
(2) How to use the char type correctly.
(3) How to use a char type input as an operator (a + b).
(4) How to use the pointer of the operator variable (+,-,*,/) in an actual equation.

Code:
#include <stdio.h>
#include <stdlib.h>
// *** Prototype Functions ***
void Post_Results (float*);
void Calculate (float*, float*, char*, float*);
void Get_Numbers (float*, char*, float*);

[Code]......

View 5 Replies View Related

C++ :: Making Namespace - Show Variables And Functions Into Compact Area

Sep 13, 2013

I know how to make a namespace i just want to know why someone would. because it just seems like a way to show a bunch of variables and functions into a compact area.

View 3 Replies View Related

C++ :: Changing Recursive Function Into Iterator Function?

Nov 29, 2014

I had the following question in my exam and received 3 out of a possible 4 marks

Here is the question:

Assume the following recursive function:
int f(int i)
{
if(i < 2)
return i;
return f(i - 2) + f(i -1);
}

Translate this function into an iterative version:

Here is my solution:

int f(int i)
{
int prev,next,final;
prev = 0;

[Code]....

View 9 Replies View Related

C/C++ :: Changing Recursive Function Into Iterator Function

Nov 30, 2014

I am trying to translate the following recursive function into its iterative function:

int f(int i)
{
if(i < 2)
return i;
return f(i - 2) + f(i -1);
}

This is what I tried to do:

int f(int i)
{
int prev,next,final;
prev = 0;
next = 1;
if(i==1)
final = i;

[Code] .....

View 1 Replies View Related

C/C++ :: Changing Recursive Function Into Iterator Function?

Nov 30, 2014

I am trying to translate the following recursive function into its iterative function:

int f(int i) {
if(i < 2)
return i;
return f(i - 2) + f(i -1);
}

This is what I tried to do:

int f(int i) {
int prev,next,final;
prev = 0;
next = 1;

[Code] ....

View 1 Replies View Related

C :: Changing The Value Of Something From A Function

Oct 30, 2013

I was talking to someone earlier about how to change the value of something from a function, and they said what was needed was to use a ** to change something, and was wondering if I could get a walk - through of what happens. I understand a single pointer well enough, but a pointer through a pointer is kind of confusing to me. Here is a simple example.

Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
int add(int ** TOP, int * stack);

int *stack = NULL;

[Code] ....

Why is it that when the program prints the address of TOP in main, it is different than the address of TOP in the function? Is it because it is a different instance of TOP because it is in the function? When I put the number on *TOP, and come out of the function back to main, it then says the address of TOP is the number entered into *TOP, and am not sure why. And the **TOP ++ at the end I am thinking it increments malloc by 1, therefore bringing the pointer TOP up to point at the next element, or am I completely off base there?

View 7 Replies View Related

C++ :: Changing Values In A Matrix Using Void Function

Jun 29, 2013

I have the following void function devised to assign "+1" or "-1" to each element of a matrix at random. While the function does what I want, when I print the values of the matrix all are set to zero:

#include <vector>
#include "aRand.h"
#include <iostream>
void initConfig(std::vector<std::vector<int> > premat, int nL, int nN) {
int * pnRand;
pnRand = 0;

[Code]...

The function pnRand_plus returns a pointer to an array of random numbers from 1 to 100, with seed time(NULL) + i. The values printed in main are zero, despite the values printed during the function run are fine (-1s and +1s).

View 2 Replies View Related

C++ :: Holding Function In Variable And Changing It Dynamically

Jun 30, 2014

I need a variable that will just hold a function that I can change in the middle of the application, even to different function type with different amount of parameters ... is this even possible? At the moment I have this

struct REPLY {
string *sReply;
function<int()> special = [](){return 0;};
REPLY()

[Code] .....

And then change it back to blank function returning 0 after calling it

(reply.special)();
reply.special = [](){return 0;};

This works fine (there is no reason why it should not, right? Now, if I want to have another function, let's say

void test2(string str) {
MessageBox(NULL, str.c_str(), NULL, NULL);
}

How do I point this one to the variable special, when I want to call it like that (or something similar)

(reply.special)("test string");

is this even possible? if so, how? i tried to create function pointer (didnt compile at all) or use template (neither did this) and how to do this as I discovered functional lib just a while ago.

View 12 Replies View Related

C :: Change / Fill List Without Changing Function Type

Feb 13, 2013

I am an IT student currently learning linked list. I have a problem with my code here. After I call addFront() my list doesn't change when I display it. How do I somewhat change/fill my list without changing the function type? I know it works on pointers still messed up with linked list.

Code:

typedef struct node *nodeptr;
struct node{int item;
nodeptr next;};
typedef nodeptr List;
}

[code]....

View 3 Replies View Related

C++ :: Changing Virtual Function Output Without Using Any Data Member

May 10, 2014

Instead of this:

#include <iostream>
struct Object {
int size; // Want to avoid this because size is (almost always) constant
Object (int s): size(s) {} // for every Object subtype.

[Code] ....

I want this:

#include <iostream>
struct Object {
virtual int getSize() const = 0;
};
struct Block: Object {
int getSize() const {return 5;} // always 5, except once in a blue moon it may change

[Code] ....

The Decorator Pattern works (getSize() can then return 6) but it is a poor choice for my program because it will cause more problems (due to many containers holding its old address, among other things. Any way to achieve this without any change of address, and without creating new storage somewhere, either inside the class or outside the class (too much responsibility to follow that stored value for the rest of the program just for this rare change, and creating a data member uses up too much memory for the many, many Block instances)?

View 5 Replies View Related

C++ :: Passing Variables Into A Function

Mar 29, 2014

ok I have a class Player with lots of variables and im gonna call a function to checkXp, if I call it with the whole player object does it use a lot more memory then if I just passed the couple things I need?

ex
checkXP(Player* play) // this is a whole object of player
or
checkXP(play->getXP(), play->getLVL()) // the variables I want.

I just realized I may not be able to modify anything from player in the checkXP() function

question 1: does passing the whole object use more memory
question 2: if I passed as just the variables I need, I wont be able to modify anything of object play?

View 1 Replies View Related

C/C++ :: Using X And Y Variables In Function Parameters?

Jan 24, 2015

In my program I created three separate return functions. Each function is labeled:

int boxes(int x, int y);
int leftOver(int x, int y);
double avgItemsShipped(int x, int y, int z);

Is it bad programming practice to use 'x' and 'y' in all of my functions? Should I use the this keyword inside the function? We use this often in my Java class and I know it exists in C++, but I haven't actually seen it used (or used it myself yet).

View 3 Replies View Related

C++ :: Declaring Variables In A Function?

Nov 12, 2012

This is a c program that is failing to compile. The error occurs in the calcLabs() function. The error called out is (btw, I'm using VS 2010): Error4error C2143: syntax error : missing ';' before 'type'

I don't understand why the compiler is not letting me declare variables in the calcLabs() function!

Code:
#include<stdio.h>
#include<stdlib.h>
void calcPercent(double *);
double calcLabs();
double calcExams();
double calcFinal();
char calcLetter(float);

[code]...

View 5 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved