C/C++ :: Modifying Array Inside Of Function
Mar 7, 2014
I am new to C++ and am trying to modify an array inside of a function.
It is of my understanding that you cannot return an array from a function?
Would writing it to a data file and then retrieving it be a solution to this problem?
View 2 Replies
ADVERTISEMENT
Mar 26, 2013
I have an array of char representing pixels in a bitmap, which I want to modify. I don't think I can just iterate over the array and pass chars into a function individually, because the function needs to take into account the neighboring pixels, too.
I thought of two ways to do this. The first would be to pass the array to the function as an argument, then have the function change it and return it. The trouble is I'm not exactly sure what happens internally when you pass an array to a function and return it. Is it the same array, modified? Or is it a copy of the array, so now you're using twice as much memory?
Alternatively I guess I could have a function with a void return type and pass a pointer to the array. I'm somewhat new to this, but the way I understand it is that a pointer is like the address of a house, while the array is the actual house. So if I give the function the address, it can go to that address and rearrange the furniture inside the house. Then, after the function returns, I can go to that address myself and see all the rearranged furniture, even though the function has already returned.
Is there a problem with the second way? It seems a bit neater, but maybe I'm understanding pointers wrong.
View 14 Replies
View Related
Jul 10, 2014
I am trying to modify a user control while modifying the main form. For example i have my main form open in one visual c# window and my user controls in another. However it seems that in order for my code changes in my user control to have any effect on the main form i need to close all my c# windows and then re-open them and even then that sometimes doesn't work am i doing anything wrong or is this supposed to happen??
Also yes i am saving and clicking build solution on my user controls.
View 14 Replies
View Related
Apr 6, 2014
Ive recently got into function pointers, i find that they can be quite handy for making your program very 'dynamic'.
However, the syntax is very confusing for what i want to do
This is what i want to do
I want to hold function pointers inside an array, but this array is dynamically allocated ( malloc, realloc, etc )
This is the current syntax ive come up with, but i dont think it is correct
void ( **drawFunc ) ( void*, SDL_Surface* );
View 2 Replies
View Related
Apr 16, 2014
I have a problem with pointer array, i passed a 2d array to a function but then i dont know how to make operations on it !!!
#include <iostream>
using namespace std;
int fun_name (int * arr) {
for (int i = 0;i< ??? ;i++) // how to compare while i don't know the size of array!!
[Code] ....
View 2 Replies
View Related
Jul 24, 2013
im tasked with creating a linear search script using functions on a 10 element array. the elements are to be supplied by the user as is the search target.
I understand how to create the array and gather that information from the user as well as howto set a variable for "target", this is what im calling it. Those two parts are simple enough.
I am not fully understanding the calling of an array in a function as a pointer. i semi understand the use of a pointer and howto call a normal pointer in a function. i also understand that an array is nothing more then a "special" pointer with a set of consecutive address blocks for the size of the array.
My first user defined function is simple enough
Code:
ReadArray(int A[], int size){
int i;
printf("Please enter %d integer numbers separated by spaces:
", size);
for (i = 0; i < size; i++)
scanf("%d", &A[i]);
}
Sso nothing out of the ordinary there. that should be a standard for loop and use of scanf, sadly prof has not covered ssanf or any of the other options so i am stuck using scanf for now. maybe someday down the line in a other program or after this course ill get the chance to learn about better options for gathering data from the user.
I am confused as to my next function:
Code:
void SearchArray(int A[], int target, int size);
I've not written any code here yet as im not sure if i should call the A[], or *A for the first type of the function?
If i call *A do i then use something like this for my search:
Code:
for (*A = 0; *A < size; *A++)
if (*A < target) or use A[] insteadA?
Code:
for (i = 0; i < size; i++)
if (A[i] = target)
View 6 Replies
View Related
Mar 26, 2014
i want to use a class to print data stored as vector or array with different data types. i also want the print function two take more than one vector or array or combination of both so that they can be written to file as two columns. so i wrote the following class:
right now it has only one member function for printing two vectors. later i'll add additional functions as required.
note: there has to be template functions inside the class
i also want the object to be global so that i need not pass it as an argument to other calling functions
class printdata
{
public:
template<typename T1,typename T2>
void SaveData( vector<T1> &data1,vector<T2> &data2, std::string var)
{
[Code]....
then i want to call this template function in another ordinary function written in a seperate cpp file
these function declarations are put in a header file. so i need know whether i should put the declaration of the template function in the header to use the function in different functions
View 4 Replies
View Related
Mar 26, 2014
i want to use a class to print data stored as vector or array with different data types.
i also want the print function two take more than one vector or array or combination of both so that they can be written to file as two columns.so i wrote the following class:
right now it has only one member function for printing two vectors. later i'll add additional functions as required.
note: there has to be template functions inside the class / i also want the object to be global so that i need not pass it as an argument to other calling functions
class printdata {
public:
template<typename T1,typename T2>
void SaveData( vector<T1> &data1,vector<T2> &data2, std::string var){
std::ofstream myfile;
std::string filename;
[code].....
then i want to call this template function in another ordinary function written in a seperate cpp file these function declarations are put in a header file. so i need know whether i should put the declaration of the template function in the header to use the function in different functions.
View 1 Replies
View Related
Dec 4, 2014
I keep on getting error message while trying to pass a function inside a for_each loop.. I have a vector and i used for_each loop to go through the rows in that vector, Now I need a function to do something
Example this is what I am trying to achieve
for_each (label.begin(), label.end(), addToTemporaryVector());
void DataPartitioning::addToTemporaryVector(){
cout<<"sucess";
}
But I get an error message saying: error: invalid use of void expression
View 2 Replies
View Related
May 1, 2014
How would I generate nodes inside a function for example I have a head node
Node * head = new Node;
Now I want to generate nodes for as long as the for loop is running
void CreateList(Node * h) {
for(int i = 0; i < 5; i++) {
Node * n1 = new Node;
head -> next = n1;
} }
Something like this but it should keep adding nodes to the list and change the head each time so how would I do this...
View 1 Replies
View Related
Jan 1, 2013
i usually use this method for accesing functions in executables, the code is executed from a DLL (always works, except when the function are inside of a class, even tho is public):
.h:
typedef int (*pgObjViewportClose) (OBJECTSTRUCT* gObj);
extern pgObjViewportClose gObjViewportClose;
.cpp
pgObjViewportClose gObjViewportClose = (pgObjViewportClose) 0x04F1940;
That works, but i can't get it to work if the accesing function is inside of a class, i get Unhandled Exception while trying to access a function inside a class, is there a way to do it?.
View 1 Replies
View Related
Aug 25, 2014
I want to have a function pointer inside a typedef struct but I get a segmentation fault when I run my code. Here's my code:
#include <stdio.h>
typedef struct Foo {
void (*bar)(int);
} Foo;
void func(int x) {
printf("display: %d
[Code] ....
View 2 Replies
View Related
Mar 3, 2013
Is this standard-compliant code?
int f() {
class C {
public:
int mf() const {return 1;}
};
C c;
return c.mf();
}
View 1 Replies
View Related
May 15, 2013
In the following code:
#include <iostream> // For stream I/O
using namespace std;
int function(int a) {
return a;
}
int main() {
function(int b);
}
Why is creating a variable inside the function argument list not allowed. Any reason other then for the language syntax or just for the language syntax?
View 19 Replies
View Related
Jul 20, 2013
Say you had:
class Foo{
public:
//...
void funky();
[Code] .....
Would each instance of Foo create a new counter variable, or would it remain the same for all of them, i.e. baz.funky() would always use the same counter variable? What if the class was a template?
View 3 Replies
View Related
Feb 18, 2014
I'm trying to split up my game (about 1300 lines) into header files, but I'm coming up with a problem whenever I try and put a function in a header file, when that function was modifying some variables that were defined before int main in the .cpp. For example:
int variable1 (0);
int variable2 (0);
void increasevariables() {
variable 1 = variable1 + 1;
variable2 = variable2 + 1;
[code].....
If it only modified one variable then I could just pass that variable and the return it:(return variable1 + 1;)But I don't know how to make a function in a header file modify several pre-existing variables. In the actual program, the variables are dependant on each other and the modifying is a lot more complicated, so I'd rather not split it into several functions and run one at a time if there's another way.
View 4 Replies
View Related
May 8, 2014
I have a class that loads the contents of a XML file into their respective object types and stores those objects in a list. Each object has its own list of objects with properties the program will later modify.
The problem I am currently having is that after looping through the objects and modifying the necessary properties the modifications do not persist. This leads me to believe I am modifying a copy of the object rather than the object I think I am modifying. I am not sure why this is happening as I believe I should be modifying the object that is referenced in the collection.
What I have tried, I started using LINQ to get the object I am looking to modify. After a little more research I don't think this can work properly due to the LINQ query returning a new object(copy of the object). Currently I am using nested foreach loops which still are not behaving the way I expect as the properties I am setting are not making their way back to the original object.
The PopulateLogSheet() in the following class is where the problem loop is.
class ChillerCheckCollection {
public List<Chiller> chillers = new List<Chiller>();
public void LoadChillers(string filePath) {
var availableChillers = (from chiller in Xdocument.Load(filePath).Descendants("chiller")
select new Chiller
[Code] ....
And here are the other classes referenced in the above class.
class Chiller {
public IEnumerable<LogSheet> chillerLogSheet = new List<LogSheet>();
public int chillerID;
[Code] ....
View 2 Replies
View Related
Nov 19, 2014
Here is the problem:
Write an algorithm such that if an element in an M * M matrix is 0, its entire row and column is set to 0.
Your function prototype may look like the following:
void setZeros ( int size, int arr[][size] );
Heres what I have:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 3
void scan_matrix(int arr[][SIZE]);
void print_matrix(int arr[][SIZE]);
void initiailize_matrix(int arr[][SIZE]);
void set_zeros(int matrix[][SIZE]);
[code]....
I'm not sure on how to scan zero for this code and I have bolded some comments that I am having trouble with as well.
View 1 Replies
View Related
May 18, 2013
This is a program I developed in which we had to define a class named BOOK with the data members and member functions as shown in the program..We have to:
(i) Make the user enter the values in the array BOOK.
(ii) Display the details that the user entered.
(iii) Search for a book from the array upon its Bno and display its details.
(iv) Search for a book from the array upon its Bname and display its details.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class BOOK {
private:
int Bno;
char Bname[20];
[Code] .....
But while running it the compiler gives the errors as:
Line 43 to 48: Illegal character '' (0x5c)
Line 69: Undefined symbol 'Display'
Line 88: 'BOOK::Bno' is not accessible.
Line 89:'BOOK::Bname' is not accessible.
Line 90:'BOOK::Author' is not accesible.
Line 91:'BOOK::Price' is not accesible.
Line 108:'BOOK::Bno' is not accessible.
Line 109:'BOOK::Bname' is not accessible.
Line 110:'BOOK::Author' is not accesible.
Line 111:'BOOK::Price' is not accesible.
from 43 to 48..the line feed was also used at many other places but there it was not given as an error so why here?
Line 69: I defined the Display() function outside the class since it contained control structures, so what's the error then?
About the lines the rest of the error( the "not accessible" ones) I know these data members are not accessible because they are in private visibility mode. But then how to make them accessible? (Without putting them in public because it was a part of the question to create the data members in private).
View 1 Replies
View Related
Nov 7, 2013
I've written an Array class to create 1d,2d and 3d array and it works fine for every test : example of the constructor of the array class for 2d case:
Array::Array( int xSize, int ySize ) {
xSize_ = xSize;
ySize_ = ySize;
zSize_ = 1;
vec.resize(xSize*ySize);
}
It works fine , but when i need to use this constructor inside of other constructor, i get the "no matching function error" ,
part of my code:
class StaggeredGrid {
public:
StaggeredGrid ( int xSize1, int ySize1, real dx, real dy ) : p_ (2,2) {}
[Code] .....
View 2 Replies
View Related
Aug 4, 2013
So, I have this code:
...
MakeTexture((char*)ilGetData(), ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT));
...
And it compiles fine and, supposedly, works fine too, but when I try to put a break point inside the MakeTexture fuction gdb just goes crazy, it freezes and starts alocating memory until it reaches like 30+ mbs, and after that codeblocks freezes and I have to terminate the gdb process to return everything back to normal.
Now, another weird thing is that this only happens if I pass (char*)ilGetData(), if I pass something like NULL to the function, this doesn't happen.
Oh, also, the MakeTexture function is this:
int MakeTexture(char *pxData, unsigned w, unsigned h)
{
return 0;
}
View 2 Replies
View Related
Jun 21, 2014
I have to read a particular student data from file using his Roll no and modify it and then save it to file again
This is the File
24 salman 98 97 96 95 94 A ;
21 faizan 88 87 86 83 85 B ;
35 Gohar 99 98 97 96 90 B ;
45 sibghat 91 92 78 85 88 C ;
009 john 89 87 91 78 95 C ;
how can i read data of roll no 35 and modify it and save it again to the file
this is my program
int r = 0 ;
while ( ar[j]!="0" ) {
//cout << "Salman majid is
" ;
if ( ar[j]==k ) {
r = j ;
getdata() ;
[Code] .....
View 2 Replies
View Related
Jun 20, 2014
I would like to understand how to rotate a 3D Object around it's Center, by modifying it's Vertex Coordinates. N
The reason is that I need to keep track of the new coordinates because of my collision system, which is based on Vertex Coords.
Here is the Code for an Object
void DrawHouse(GLfloat x1, GLfloat x2, GLfloat y1, GLfloat y2,
GLfloat z1,GLfloat z2, GLfloat u1, GLfloat u2, GLfloat v1, GLfloat v2, GLuint textid) {
glBindTexture(GL_TEXTURE_2D, textid);
glBegin(GL_TRIANGLES);
glNormal3f( 0.0f, 0.0f, 1.0f);
glTexCoord2f(u1,v1); glVertex3f(x1,y1,z2);
[code]....
View 7 Replies
View Related
Nov 22, 2012
I've got
[tranlated]
Code:
error C1083: cannot open: '[!output PROJECT_NAME].h': No such file or directoryd:program files (x86)autodesk3ds max
2012maxsdkhowto3dsmaxpluginwizard emplates1033atmospheric_type_atmospheric.cpp1513dsmaxPluginWizard
I opened the project
Changed it to vc100
Changed it to x64
Disabled optimization
And compiled
And the results are shown above
View 1 Replies
View Related
Jun 7, 2013
Alright, so I have a code that's not giving me errors, but it doesn't seem to retain what I put into an array. Not sure If I'm missing something...
Code:
#include <stdio.h>
int main (void)
{
const char *pointer;
const char alphabet[] = "ABCDEFG";
pointer = &alphabet[5];
printf("pointing to %c of the alphabet
", pointer);
return 0;
}
Trying to get my pointer to return the letter in the [5] spot or "F". Not receiving any errors when compiling, but I seem to get different answers every time I run it.
View 5 Replies
View Related
Apr 18, 2013
class Hallway {
private:
//---------------------------------------------------------------
// DO_04: Declare a Light array of size MAX_LIGHTS
// Hint: look through the methods below to find the name to use
// for the array
//---------------------------------------------------------------
int numLights;
int lights[MAX_LIGHTS];
[Code] .....
I keep getting the error " this.lights[i] is not a struct or class and so you cannot use '.' " on line 34.
How am I supposed to define my lights[] array? I can't modify anything except directly after each comment block.
View 6 Replies
View Related