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


ADVERTISEMENT

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 :: 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 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++ :: Changing The Value Of A String?

Dec 17, 2013

If I was to input for example 'x' into my program, how could I change that to something like 'HuS581' every time that specific character was inputted?

View 2 Replies View Related

C++ :: Changing Const Int Value?

Nov 7, 2013

#include <iostream>
#include <iomanip>
using namespace std;

// cin.get() <-------------- used to let the user read the screen

// Function prototypes
void calcSales(const int [], const double [], double [], int);
void showOrder(const double [], const int [], int);

[Code] ....

How can i change the "const int NUM_PRODS = 12;" from saying id 1, id 2, id 3, etc. to custom product numbers?

View 1 Replies View Related

C++ :: Changing Some Bit Of A Number

Jan 1, 2015

I use miracle library to work with big numbers. I defined a number for example X with 192 bit. at first X= 0 ;I want to change for example 3 bit of it to 1 randomly.as a example:

assume that X has 10 bit :
X=0000000000
random numbers should be :
1000010001
1010100000
1100000100

View 1 Replies View Related

C/C++ :: Changing Complexity From O(n) To O(1)

May 5, 2012

For the following code :

 s = 0 ;
 for(i=m ; i<=(2*n-1) ; i+=m) {
            if(i<=n+1)
            { s+=(i-1)/2 ; }
           else
            { s+=(2*n-i+1)/2 ; }  
      }  

I want to change the complexity of the code from O(n) to O(1) . So I wanted to eliminate the for loop . But as the sum "s" stores values like (i-1)/2 or (2*n-i+1)/2 so eliminating the loop involves tedious calculation of floor value of each (i-1)/2 or (2*n-i+1)/2 . It became very difficult for me to do so as I might have derived the wrong formula in sums of floors . Need Changing complexity from O(n) to O(1). Is there any other way to reduce the complexity ? If yes ... then how ?

View 3 Replies View Related

C++ :: Changing From BigInteger To GMP?

May 15, 2012

I have this code which previously used the BigInteger library, however now I have GMP installed and I want to use that instead.

I have read the manual but I cannot figure out how to use GMP in this function without getting errors.

Here is the function:

Code:
int lucas(long p){ //p is a number in the range of 2 up to 50,000,000, possibly bigger
int s = 4; //previously assigned as a big integer
int z; //used in the for loop below
int M = 2; //previously assigned as a big integer
for(z = 1; z < p; z++){ //this accomplishes the same as 2 to the power of p, and is stored in M
M *= 2;
}
M--;

[code]....

I can initialize variables using the gmp library, but when I'm trying to use the mpz_pow_ui() function I get errors because it wants me to use long integers, which are too small for the numbers I want to work with.

How can I re-write this function to use GMP?

View 4 Replies View Related

C :: Changing Value Of A String Through A Pointer

Jun 11, 2013

Basically, I have a pointer to a C string:

Code: char **objectData and a C string:

Code: char temp[350]; I need to assign the CONTENTS of the temp to the CONTENTS to which objectData points.

Code: *objectData = temp; //this changes the pointer. When temp is deleted, objectData points to some rubbish

**objectData = *temp; //this doesn't seem to be doing anything - the string to which objectData points does not change.

View 3 Replies View Related

C :: Changing Program To Application?

Aug 22, 2014

I was wondering if there was a possible way to change my c program into an application so that i can send the application to another person and not let the other person have access to my codings?

My reasons for asking this question: For the application i want to send there are secrets that are meant to be unraveled by gameplay but i dont want people to look at the codes and know everything.

View 6 Replies View Related

C++ :: How To Get Numbers In Order But Not Changing Name It Goes With

Mar 13, 2014

SO lets say I have a file that says

10 tennent
9 Eccleston
12 Capaldi
11 Smith

How do I get the number's in order but not changing the name it goes with? SO here is how I started it

# include <iostream>
# include <fstream>
# include <sstream>
# include <set>
# include <list>
# include <string>
# include <cctype>
# include <vector>

[Code] ....

View 12 Replies View Related

C++ :: Changing Array Values

Jun 15, 2013

If I have an array of two columns that have the same values and I want to change only the third column how can I go about doing this. The values of the third column will change based on the values in one of the columns which I plug into a math equation. Also how come I can't show a double value in the array?

#include <iostream>
#include <math.h>
#include<iomanip>
#include <vector>
using namespace std;

[code]...

View 7 Replies View Related

C++ :: Changing Variable In For Loop

Apr 28, 2013

I have a problem with my code which I can't work out:

double Mi = 200*pow(10,30);
cout << "
Enter accreted mass increment in solar masses
";
cin >> dm;
cout << "

[Code] ....

Basically the loop works, but gives the wrong results.

I need, at the end of the loop, to sort of "redefine" Mi as "Mi + Macc". I then need it to repeat the loop, and at the end add another Macc so that Mi becomes "Mi + Macc + Macc", etc.

View 5 Replies View Related

C++ :: Changing String To Bool?

Jan 21, 2015

I want to change string to bool but I'm having trouble doing this.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int check_Pythag(int DIMA, int DIMB, int DIMC) {

[Code] ....

View 2 Replies View Related

C++ :: SFML 2.0 Changing Alpha?

Sep 18, 2013

I want to change the alpha of a sprite without changing any other colours

View 4 Replies View Related

C# :: Changing Focus Using Program PID

Apr 17, 2014

For the past couple days I've been making small improvements on a program I made to keep track of a battle count while playing a game. Mainly to reduce the chore of keeping track yourself while you're playing. But it still becomes a hassle to switch back and forth between the program and your game and I was hoping to be able to set focus to the game after the BattleCount method is called.

Currently I am importing a DLL that somebody recommended I use to make a ActiveProcess method that accepts a integer as a parameter which would set the PID but it doesn't seem to be working. The two methods are as follows.

private void countDownClick(object sender, RoutedEventArgs e) {
//Calls a method within my class that subtracts and adds battle values.
counter.CalculateBattles();

//These two change text on my WPF forms with accessor values from my class.
battlesWonText.Text = counter.BattlesWon.ToString();
battlesLeftText.Text = counter.BattlesLeft.ToString();

[Code] ....

This is the C++ code they told me to use to set focus to another application using a PID.

[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
const UInt32 SWP_NOSIZE = 0x0001;
const UInt32 SWP_NOMOVE = 0x0002;
const UInt32 SWP_SHOWWINDOW = 0x0040;

[Code] ....

I'm simply trying to figure out how to set focus to a foreign program using their PID and a button click event in WPF. I'm using Visual Studio 2012 on Windows 7.

View 4 Replies View Related

C# :: Changing Database Values?

Dec 14, 2014

I'm working on a project where we access a tiny premade database in order to log into and access another page. I got the log in part working right, but the last part of the project asks me to lock out the account after a certain number of failed attempts. I'm not sure if I need a method to do this or if I can do it right there in the button handler. I tried creating a method to use on the object that was already there, but I'm not sure if that's the right way to go about it. It says it locks the account, but the value in the database never changes, and I don't know why.

//from the login button handler
protected void btnLogin_Click(object sender, EventArgs e) {
bool IsFound = false;
clsDataLayer dl = new clsDataLayer();
IsFound = dl.GetUser(Server.MapPath("~/AddressBook.mdb"), txtUserID.Text, txtPassword.Text);
if (IsFound) {
Response.Redirect("~/frmUpdateAddress.aspx");

[code]....

View 14 Replies View Related

C# :: Changing IP Address On Controller LED

Jul 2, 2014

I have a C-Power5200 driver. I want using the C# language change to the controller's IP address.

Here a link to the website and API.C-POWER 5200

In Annex API controller.

View 7 Replies View Related

Visual C++ :: Changing Code From TCP To UDP

Mar 20, 2013

i had created a client server program in MFC using TCP but by doing this the server can only work on one computer because you have to tell the client the ip address to connect to i.e

Code:
ServerAddr.sin_addr.s_addr = inet_addr("10.13.31.112");

so i want to change my code so that i can use broadcasting but i found out that it only works using UDP. I tried changing the code but the error message "error with sendto: 10047" displays when i run the program.

that error means

Address family not supported by protocol family. An address incompatible with the requested protocol was used. All sockets are created with an associated address family (that is, AF_INET for Internet Protocols) and a generic protocol type (that is, SOCK_STREAM). This error is returned if an incorrect protocol is explicitly requested in the socket call, or if an address of the wrong family is used for a socket, for example, in sendto.

here is the code from server:

Code:
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2), &wsaData);
int port = 7171;
if (param)
port = reinterpret_cast<short>(param);

[code]....

View 1 Replies View Related

C :: Changing Cents Into Individual Denominations

Sep 16, 2013

I haven't actually learned how to use arrays yet but I know the basic principle of it and decided to try and implement one to improve my code. I'm sure the thing is a bug ridden mess but I would particularly like to point your attention to the function sortDenomination(). Is what I am trying to do in the loop possible?

The reason why I want to do it this way and not with if statements for each denomination is because I can then easily apply this to the second part of the assignment which sees us split a double into dollars and cents and then process each separately. All I would have to differ for each is change the money_loop variable to include the last two denominations in the array.

Code:
#include <stdio.h>
/*Reads cents input from the user.*/
void getCents(int &read_cents) {
printf("Please enter the amount of cents between 5-95:

[Code] .....

View 10 Replies View Related







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