C/C++ :: Assigning Pointer (int) Located In Main With Another Function

Aug 2, 2014

I have the following:

int Allocate(int, int *);
main() {
int *Pointer;
int Elements = 25;
// this works just fine - as expected.
Pointer = (int *) malloc(Elements, sizeof(int));
// This DOES NOT - The value of Pointer never changes.....

[code]....

View 2 Replies


ADVERTISEMENT

C++ :: Using A Member Within Pointer Function (that Is Located In Same Class) - Segfault

Jan 6, 2013

I'm currently programming a server which uses multiple threads- I have a class for one map in the game. Each map has a thread for timed events(tile regeneration, NPC regeneration, etc.), and a thread for handling NPCs(movement, combat, etc.). A basic structure of the class looks like this:

class Region {
public:
/* game values are here, they are public so
they can be accessed from outside of the class
inside of packet-handling functions and such */
int value;
void *Function();

[Code] ....

The program crashes when I use a member of the same class the function is located in- in the context I have shown about it would crash on "value++".

View 11 Replies View Related

C++ :: Pointer Function - Getting Information Back To Main

Feb 6, 2014

I am trying to make a information to pointer function but the problem is how/what number am i putting in for *a...

And can i get that information back to main? and is that right way to split the array in to ? because i have will 200 random number and I need to find the smallest and second smallest element in array.

two_smallest (print, size3,size3,size3,size3);
} void two_smallest (int find_array[] , int size3, int *a, int *a2, int *b,int *b2) {
for (int index = 0; index <= 100; index ++) {
if (find_array[index] < find_array [0]) {
find_array[0] = find_array [index];
*a = find_array[0];

[Code] ....

View 1 Replies View Related

C/C++ :: Converting Int To Double Pointer Outside Main Function

Dec 22, 2014

I have int pointer called p and i want to calculate average.Since average involves using double or float so i am trying to convert this in the function averagetemp. It still gives me an error saying "cannot be converted"...

#include <iostream>
using namespace std;
int* createArray(int n);
int lowesttemp(int *p,int f);
int highesttemp(int *p,int f);
double averagetemp(int *k,double f);
void print(int *p,int lowest_temp,int highesttemp,int average_temp);

[Code] ....

View 8 Replies View Related

C/C++ :: How To Get Formation From Pointer Function To Main Function

Feb 13, 2014

int example (int [], int, *int,*int,*int,*int);
int main () {
My code will be here
example (int array[], int size, &a,&b,&c,&d); // Like this??? I try it didnt work

[Code] ....

View 2 Replies View Related

C :: File Opened In Main And Passing Pointer To A Function

Sep 14, 2013

I am getting a few compile errors for what might be a simple thing to do. I am opening a file in main, passing that pointer to a function and checking the contents of that file with a regex before I pass it on to build a BST. I am getting the following compile errors, what is wrong. Here are the errors:

Code:
gcc main.c fileCheck.c -o tree
main.c: In function `fileCheck':
main.c:19: error: syntax error before "FILE"
fileCheck.c: In function `fileCheck':

[Code] .....

Fatal error: Command failed for target `tree' Here is the two files and header that seem to be causing me the problems.

main.c

Code:
#include "main.h"
//#include "node.h"
int main(int argc, char *argv[])
FILE *fp;
if (argc > 2)

[Code] ....

And the header file.
main.h

Code:
#ifndef MAIN_H
#define MAIN_H
#ifdef __cplusplus
extern "C" {
#endif

[Code] .....

View 2 Replies View Related

C :: Correct Way To Call A Function Within Main That Has File Pointer Parameters?

Mar 16, 2013

What would be the correct way to call a function within main that has file pointer parameters?

function prototype: Code: void CalculateBoth(int num1, int num2, int*sumPtr, int *diffPtr);

View 2 Replies View Related

C++ :: Pointer Usage To Base Class Object In Vector Int Main Function

Aug 12, 2013

I know what are pointer's and how to use them but there is one point i am not able to understand. Below is the example code

I understand everything in the below code except 1 thing why i am using pointer to base class object in vector int the main() Function?

Code:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
// base class

[Code] ...

Here is the lines of code i want to understand.

Code:
vector<Employee*> employees;
employees.push_back(&emp1);
employees.push_back(&mgr1);

I know if i will not use the pointer base class function "virtual double grossPay" will be called for both base class object and derived class object and when i will use pointer with reference to the object because base class function is virtual it will look for same function in derived class and if available it will execute it.

View 3 Replies View Related

C :: Passing Argument Of Incompatible Pointer Type - Warning In Function Call In Main

Jun 4, 2013

Code:
#include <stdio.h>
#include <stdlib.h>
int size_b_row1;
int size_b_col1;

[Code].....

View 2 Replies View Related

C++ :: Passing Arguments From Incompatible Pointer Type - Warning In Function Call Transpose In Main Routine

Jun 4, 2013

#include <stdio.h>
#include <stdlib.h>
int size_b_row1;
int size_b_col1;
int main(void) {
double C[4][4] = {{1,3,5,2},{7,6,2,2},{1,2,7,3},{2,3,5,3}};
double TC[4][4];
transpose(C, TC);

[Code] ......

View 2 Replies View Related

C :: Assigning Value To Char Pointer

Feb 3, 2013

I thought we needed to allocate memory before assigning a value to a char* and also that we needed to use functions like strcpy() to copy something into it. Then how come this works and does not crash?

Code:
#include <iostream>
using namespace std;
int main()
{
char * buf;
buf = "Hello";
cout << buf << endl;
buf = "World!!!!!!!!";
cout << buf << endl;
return 0;
}

View 3 Replies View Related

C :: Assigning Value During Pointer Declaration

Aug 31, 2013

I am trying to understand the behavior of following code. Basically how does printf() prints the value rather than address.

Does initializing value to a pointer during declaration makes a difference when assigned from a variable?

Code:

1 #include <stdio.h>
2
3 int main() {
4 const char *var1 = 'A';
5 int *vint = 10;

[Code] ....

View 8 Replies View Related

C++ :: Using Pointer In Union And Assigning Value

Jul 2, 2013

In the current code,We are using pointer of union and assigning value.

class sample {
union {
short *two_int;
int *four_int;
double *eight_real;
char *one_ascii;
// void *v;
}; }

Than we assign value in following way.

sample.four_int[0] = (x + xoff); ( x and xoff and y and yoff all are integer)
sample.four_int[1] = (y + yoff);

Than we write data into file. it was working fine into 32 bit machine but it is not working 64bit machine. When I compare data and found that data is divided by 4. For Ex The File generating from 32 bit machine contain 80 than 64 bit . File contain 20.

View 7 Replies View Related

C :: Assigning A Pointer To A String Literal

Sep 13, 2013

In another forum, this example code fragment was stated as being an example of undefined behavior. My understanding is that a literal string exists from program start to program termination, so I don't see the issue, even though the literal string is probably in a different part of memory.

Code: /* ... */
const char *pstr = "example";
/* or even */
char *pstr = "example";
/* as long as no attempt is made to modify the data pointed to by pstr, */
/* unless pstr is later changed to point to a stack or heap based string */

View 11 Replies View Related

C++ :: Assigning Address To Object Pointer

Aug 16, 2013

I have the following code.

StackElement *StackElementArray;
StackElementArray = new StackElement[iMaximumDepth];

I want to assign one element of this array StackElementArray the address of another object. For example,

voidStackMem::push(StackElement &iStackElement) {
CurrentDepth++;
StackElementArray[0] = iStackElement;
}

The StackElement class contains pointers to some dynamic arrays. When I use the assignment, StackElementArray[0] = iStackElement;, it doesn't copy the complete contents and I have to define an 'assignment operator' function to copy all the contents. I am wondering if there is a way I can assign StackElementArray[0] the pointer to the StackElement object. In that case, I will not need to copy the contents of iStackElement into StackElementArray[0] and will just copy the address.

View 3 Replies View Related

C++ :: Assigning Pointer In Node To Point To New Vector

May 9, 2013

I've got a struct called Node that contains, among other things, a pointer to a vector of pgm objects. (pgm is a class i've created)

struct Node {
int label;
vector <pgm> *ptr;
Node* lessNode;
Node* moreNode;
};

in another class, i create a vector and a Node and am having trouble assigning the pointer in the Node to point to my new vector.

vector <pgm> lessData;
Node* left;
left->ptr=&lessData;

This all compiles ok, but the last line in the code above causes a segmentation fault. I should mention Node is declared on its own in Node.h and what pgm is. including pgm.h in node.

View 2 Replies View Related

Visual C++ :: Error Assigning Pointer To Iterator

Nov 27, 2014

im trying to port a code from vc6 to vs2013 and im having this error

Code:

Error11error C2440: 'initializing' : cannot convert from 'char *' to 'std::_Vector_iterator<std::_Vector_val<std::_Simple_types<char>>>' on this line

Code:
vector<char>::iterator BufIt = (char*)lpBuffer;

what i do with this is to stack fragments of data of type char* coming from a socket in buffer to a vector that acts as buffer, I do this since I transfer big chunks of data and the data gets fragmented by the nature of the sockets, I stack the data once its complete I retrieve the final result from the vector.

this code worked flawlessly for long time but now Im trying to port and compiler throws this error, whats the new way to assign a char array pointer to a iterator so i can stack it in the vector.

View 5 Replies View Related

C++ :: Main Difference Between Pointer And Reference And How To Use Pointers

Mar 9, 2013

i'm still unclear between the difference between using pointer and a reference

I understood the concept of pointers in c in the class i took last year

and that was to change the actual value stored in the memory address Code:

void change_a(int a*){
a=6;
}
int main(){
int a=5;
change_a(&a);
}

but in c++ I've been using references in all my assignments because I don't know how to correctly use pointers in c++ I may have missed a class but I'm on spring break and would like to clear things up

so in c++

in my assignments I would call it like this Code:

void change_a(int &a){
a=6;
}
int main(){
int a=5;
change_a(a);
}

so does this change the value in the address or does it make another copy of a in my c++ code and stores 6 in that copy

View 2 Replies View Related

C++ :: Keyboard Function That Is Called In Main Function To Make Shape Move

Jan 19, 2013

Ok so I am working on a game and I'm in the process of developing my Player class. Anyways, what I have is a keyboard function that is called in my main function to make a shape move.

void myKeyboardFunction(unsigned char key, int x, int y) {
switch ( key ) {

[Code].....

But when I try to call it, trying to copy my previous method,

glutKeyboardFunc(Player1.playerControls);

I get an error

error C3867: 'Player::playerControls': function call missing argument list; use '&Player::playerControls' to create a pointer to member

I get an error saying it can't convert parameters. I would just like to understand why the arguments become a problem when I make the function a member of my class, when the first method I used is so easy.

View 2 Replies View Related

C++ :: Assigning A Function Using Boolean Operator

Nov 18, 2013

I am writing a code where I have to find out the spot a letter is in. I am getting an error with assigning a function using a boolean operator.

bool is_member(const vector<char> & list, char character) {
for(int i=0; i < list.size(); i++) {
if(character==list[i]) {
return(true);

[Code] .....

My constant vector list is { 'a', 'e', 'i', 'o', 'u', 'y'}. My error comes in on line 20. I am not calling the boolean correctly. If I type in the letter "i". Then the function should output 2 since i is in the 2nd index spot of my vector list. How to fix my error? I am not understanding why my line of code is not working.

View 10 Replies View Related

C :: Calling Function Via Function Pointer Inside Structure Pointer

Mar 14, 2013

I'm trying to call a function via a function pointer, and this function pointer is inside a structure. The structure is being referenced via a structure pointer.

Code:

position = hash->(*funcHash)(idNmbr);

The function will return an int, which is what position is a type of. When I compile this code,

I get the error: error: expected identifier before ( token.

Is my syntax wrong? I'm not sure what would be throwing this error.

View 3 Replies View Related

C :: Main Function Does Not Return Any Values When Calling Other Function?

Jun 9, 2013

The function is supposed to return value from the file in my main, but I am getting empty value. I am trying to get better with pointer. Right now just teaching myself.

right now the only way for this code to show value is when in put the putchar(*ps) inside my readfile function. I would like to readfile to return value and print in the main function.

Code:

#include <stdio.h>
char *readfile(char filename[]);
int main(int argc, char *argv[] ) {

[Code].....

View 4 Replies View Related

C/C++ :: Assigning Functions To Array Of Function Pointers

May 13, 2013

I'm trying to create an array of function pointers and then assign compartilbe functions to them, so I can just call *pf[0](xxx);

The functions are all of the type

void func01(unsigned char*, int, int)

how would I create an array of function pointers and assign the address of the functions to them? So I could call them like

ptrToFunction[i](charBuffer, 10, 20);

I've read a bit on line and I thought I could do it but so far I've failed.

It seems trivial and I feel I'm close but close isn't good enough.

I'd like to assign the fuction addresses like this:
for (int i=0; i<10; i++)
if (i==1)
ptrToFunction[i]=func01;
if (i==2)
ptrToFunction[i]=func02;
etc.

The actual logic is somewhat different than this but this close.

View 4 Replies View Related

C :: How To Pass Main Function Argument To Some Other Function

Dec 26, 2014

I am writing a program in which a Fucntion has to be wriiten to parse the Command Line . When I include Code for parsing in main fuction iteslf ,its run ok . But I want to make a fucntion of that code and call it from main ,than it show Segmentation error .

By using Debugging I found Some thing is mess with " -m" Parameter of Command line , But Cant Rectify it ..

Code:
int main (int argc, char *argv[]){
//get_parameter_value(argc,argv);
// buffer[packet_size+1]= char ("'");
while (argc > 1) {
if (argv[h][0] == '-')

[Code] .....

View 3 Replies View Related

C :: Pointing To A Vectors X Coordinate Located Within A Structure

Feb 8, 2015

Cam is a pointer to a structure and viewpoint is a vector located within the struct. I am trying to read in from a file the coordinates for the vector. I have also tried &cam->view_point->x as well as &cam.view_point.x and it tells me that I am requesting something not in a struct

Code: count= fscanf(in,"%d %d %d", &cam->view_point.x, &cam->view_point.y,&cam->view_point.z);

View 14 Replies View Related

C :: DLL Error - Procedure Entry Point Could Not Be Located

Oct 22, 2013

I hope that this isn't an elementary question to you more experienced people, but I am having some trouble with my .DLL file. I haven't ever had a problem with compiling and linking it with my main project before, but it appears now some sort of problem has started. The message that appears is :

I have no qualms with posting my .DLL source, as it is not particularly long or complex. It seems the problem functions are the last two I've added ( load_sound( ) and play_sound( ) ).

Here is what I'm linking it with :

Code:

-------------- Build: Debug in game (compiler: GNU GCC Compiler)---------------
mingw32-g++.exe -shared -Wl,--output-def=binDebuglibgame.def -Wl,--out-implib=binDebuglibgame.a -Wl,--dll -L"F:FBLARandom gamegame.dll.sourcegame" objDebugconversion.o objDebugprojectile.o objDebugSDL_utility_functions.o objDebug ime.o -o binDebuglibgame.dll -lSDL -lSDL_mixer
Creating library file: binDebuglibgame.a

Output size is 49.10 KB
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 0 warnings (0 minutes, 0 seconds) projectile.c :

Code:
#include "game.h"
#include <stdio.h>
#include <stdlib.h>

/* ******************************************************** */
/* ********* create( ) ********* */
/* ******************************************************** */
/* - Allocates one node of type projectile on the heap, */
/* and returns its' address */
/* ******************************************************** */

[Code] ....

View 6 Replies View Related







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