C++ :: Enforcing That A Function Does Not Modify Dynamic Array Pointed To By Pointer

Aug 21, 2014

In case the question was not very clear. Say I have the following code:

int sum(int * x, size_t N){
int sx;
/* returns sum of elements in x */
return sx;

[Code] ....

I'm curious to know whether there is a way that ensures a function does not modify the memory pointed to by a pointer.

View 9 Replies


ADVERTISEMENT

C++ :: Why Cannot Copy String Pointed By Pointer To Array Of Char

Feb 22, 2013

I have this function in a class: and a private declaration: how can I copy the parameter "ProductName" to allowedProductName. I tried all combination and I can't get it to compile.

private:
StatusPanel &statusPanel;
char allowedProductName[MAX_NAME_LENGTH];

[Code].....

View 9 Replies View Related

C++ :: Delete Member Of Vector That Is Pointed By Pointer?

Apr 17, 2013

I have following:

struct Point {int* a; int b;};
vector<vector<Point> > numbers;
vector<int> example;

The numbers vector has a matrix of a sort and each of the members are pointing to one member in the example vector. A member numbers.at(2).at(3).a is pointing at example.at(3). Now, can I remotely delete a member in the example vector using the pointers? Like so:

delete (*(numbers.at(2).at(3).a));

I know there is a more convenient way to delete members, but this is a very specific case I'm working on.

View 3 Replies View Related

C :: Modify Value Of Whole Array By Passing It To A Function - Invalid Lvalue Error

Sep 23, 2013

i want to modify value of whole array by passing it to a function and make each value of array multiplied by 3 ,return the value to main and print it using pointer.

error : invalid Lvalue is the error

Code:

#include<stdio.h>
main()
{
int i,arr[10];
for (i=0;i<=9;i++)
{
printf("Enter value of arr[%d] : ",i);
scanf("%d",&arr[i]);

[Code] ....

View 1 Replies View Related

C++ :: Creating A Pointer To Dynamic Integer Array?

Sep 15, 2013

Create in the private section of the ServerGroup class a pointer to a dynamic integer array called servers.

#ifndef ServerGroup_h
#define ServerGroup_h
class ServerGroup {
public:
private:
int *ptr = new int; // needs pointer to a dynamic integer array called servers
};
#endif

View 2 Replies View Related

C++ :: Create Dynamic Pointer Based Array Of Class

Nov 29, 2014

I have a class called Book and I am trying to create a dynamic pointer based array of the class. When I try to run the program I keep getting the error: pointer being freed was not allocated at the line of code that says "delete [] A;". I am using Xcode to run the program.

Book *changeArraySize(Book *A, int &size, double factor) {
int i;
int newsize = size*factor;
Book *A2 = new Book[newsize];

[Code] ....

View 7 Replies View Related

C/C++ :: Create User Defined Dynamic Array For Player Scores - Missing Pointer Types Error

Jan 18, 2015

I'm completely new to pointers and have a homework assignment due where I'm supposed to create a user defined dynamic array for player scores. The only errors I'm experiencing is a C2109: subscript requires pointer type and only on the lines that use the int *score; variable (57, 62, 64, 69, 71, and 82). I've already tried adding = nullptr to the variable and that didn't work.

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
void players(string[], int[], int);
void average(int[], int);

[Code] ....

View 3 Replies View Related

C :: Dynamic 2D Array To Function?

Jul 15, 2013

Code:
Int** d = malloc( ROWS * sizeof(int*));
for (i = 0; i < ROWS; i++)
d[i] = malloc(COLS * sizeof(int));
fx(d);

My question is, in a function declaration, why do I not have to specify the number of columns. How is this different than when I pass a static 2D array to a function, in which I must declare the function parameter with the number of columns.

Code: void fx(int d[][COLS]);
VS.
Code: void fx(int **d);

View 7 Replies View Related

C++ ::  Dynamic Array Resize Function

Apr 22, 2014

I made a resize function for my dynamic array template class that goes as follows. Note that the private member variables are T* arr, unsigned used, and unsigned cap.

template <class T>
void darray<T>::resize(unsigned size) {
if (size > cap) {
T* temp_arr = new T[size];

[Code] ....

Whenever I use this function to increase the size of the array, it will work the first time I need to use it, but after that, Visual Studios will trigger a breakpoint at line 14, and if I continue past the breaks, I eventually get _CrtIsValidHeapPointer(pUserData) assertion failure.

View 4 Replies View Related

C++ :: Passing Dynamic Array To A Function

May 8, 2014

I am trying to pass a dynamic array to a function which will:

- Copy the contents of the array to a temporary dynamic array
- Change the array passed in to one size bigger
- Copy the elements from the temp array back into the newly changed array
- Insert an item into the last spot of the array

Here is my code:

#include <iostream>
using namespace std ;

void make_array ( int Old [] , int & old_size , int toInsert ) ;
void zero_array ( int arry [] , int arry_size ) ;
void print_array ( int arry [] , int arry_size ) ;

[Code] .....

The output seems like a memory address but is just a very large number, what have I done incorrectly to cause this?

View 2 Replies View Related

C :: 1D Dynamic Array Crashes When Read From File Into A Function

Mar 17, 2014

code:

/part of main function
int *A=NULL;
//load from file
load_from_file(A, &next);
printf("next=%d
",next);
getchar();
printf("A[%d]=%d
",0,A[0]);getchar();//here program crashes
}

[code]....

I initialize array A with NULL in main, and I "load" it with elements from a file. The code without function is working. But when I tried to use a function as above, my array crashes!!!

View 4 Replies View Related

C++ :: Timing Function - Sort Dynamic Array With N Elements

Apr 11, 2014

I'm writing a program that will implement BubbleSort and MergeSort and time them as they sort a dynamic array with N elements. These are the instructions on what my main.cpp file should do.

main.cpp
Include all needed libraries for timing and random number generation while the number of element, N, is less than 100001 repeat the following.

create an array with N (initially 10) random elements
sort the same array with Bubble and Merge sort
time how long it takes each algorithm in microseconds (see example below)
Increase N by a factor of 10 (N *= 10)
Hint: you may want to put your merge sort object on the heap and delete it with every iteration of this loop

And this is what I have so far for my main.cpp

#include <iostream>
#include <sys/time.h>
#include <ctime>
#include <cstdlib>
#include <stack>
#include <iomanip>
#include "BubbleSort.h"
//#include "MergeSort.h"

[Code] .....

One of the errors that I'm running into is that I'm not sure how to correctly call the function I think?

View 3 Replies View Related

C :: Dynamic Array From Function To Function

Oct 19, 2014

my program is just counting the maximum of the array, however, I'd like to do that with the dynamic array (with pointers).

Code:

#include <stdio.h>
#include <stdlib.h>
void ArrayScan(int n, int *h);
void ArrayMaximum(int n, int *h, int x);
int main()
}

[code]....

View 3 Replies View Related

C++ :: Write A Function To Modify The Input String?

Oct 8, 2014

i have to write a function to modify the input string by substituting each alphabetical character. for example, 'Programming' to 'Rtqitcookpi' (mod_int = 2).

View 1 Replies View Related

Visual C++ :: How To Modify System Menu To Execute A Function

Feb 11, 2013

I wish to add the item 'Website' to an MFC Dialog app System Menu so that it can activate the following method:

Code:
void CMyDlg::OnWebpage()
{
ShellExecute( NULL, NULL, _T("[URL]..."), NULL, NULL, 0 );
}// OnWebpage()
I have tried this:

Code:

#define IDM_WEBSITE (WM_USER + 101)
//..
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
pSysMenu->AppendMenuW(MF_STRING, IDM_WEBSITE, _T("Website"));
}

[Code]...

The code compiles without error but and the 'website' item appears on the System Menu but it doesnt do anything.

I've inspected the following but don't really understand how it works and it seems to me there should be a simpler way to accomplish my end.

Modifying the System Menu By John Simmons 26 Jan 2007 [URL]....

View 8 Replies View Related

C++ :: Create Main Function With One Dimension Dynamic Array With Float Data Type

Dec 4, 2013

I need to create a main function with a one dimension dynamic array with float data type. The total number of array elements must be controlled by a user input from the keyboard. Test data is three different lengths 3,6,9 of the array. The lengths have to be set up at run time from users input. I understand how to create dynamic array but not where the user inputs the length of the array. How would I implement this?

View 6 Replies View Related

C :: Segmentation Faults When Trying To Modify A Character Array

Apr 27, 2013

I've initialized a character array in my main, and passed it onto one of my functions convertToPostfix(char infix[], char postfix[]). It isn't a constant read only array (constant char*) ive explicitly initialized it to char postfix[] = "";

Code:

if ((nextChar > 47) && (nextChar < 58)){ //if its a digit
postfix[++postfixIndex] = nextChar;
while (infix[i + 1] > 47 && infix[i + 1] < 58){
nextChar = infix[++i];
postfix[++postfixIndex]=nextChar;//segmentation fault when assigning from infix to postfix**
}

View 3 Replies View Related

C++ :: Fill Value Of Dynamic Array Of Dynamic Arrays?

Jan 4, 2013

I'm writing a program in which I have to use a matrix to represent a file in code. because it's a file, the size of the matrix is undefined, and therefore the matrix has to be dynamic. I found out that my compiler doesn't like dynamic multidimensional arrays, so I was thinking of this matrix as a dynamic (monodimensional) array of other dynamic (monodimensional) arrays. My program (and thus this example) uses unsigned chars.

unsigned char variable=something;
unsigned char**matrix=new unsigned char*[lenghtOfMainArray];
for(int rowNumber=0;rowNumber<lenghtOfArray;rowNumber++)
{

[Code].....

View 2 Replies View Related

C++ :: Modify Contents Of 1D Character Array - Converting To Upper Case

Feb 10, 2015

I am unsure how to write a function which modifies the content of the 1D character array and puts all of the letter it contains into uppercase. the following are the letters which i am trying to convert.

char text[MAX+1] = {'T', 'e', 's', 't', 'e', 'r', EOT};

The output to this should look like T E S T E R EOT

View 2 Replies View Related

C++ :: Double Pointer - Dynamic Allocation

Oct 24, 2013

I'm writing a program involving a theoretical hotel. Most of the code is already written, but the part I'm having trouble with involves the very beginning of the code.

The program is designed to read in from an input file the hotel's ID number, the types of rooms it has, the Room Numbers of each room, the base rate for each room, and the rate of additional charge per person.

The code demonstrates inheritance for my Object-Oriented Programming class, as each type of room will inherit from generic class Room, but I can't seem to figure out how to make the double-pointer for dynamic allocation work.

The code is below.

Hotel.h
Code:
class Hotel{
int hotelID;
static const int MAX_SIZE = 101;
Room **rPoint;

[Code] ....

View 11 Replies View Related

C++ :: Pointer To Point Dynamic Arrays?

Jan 31, 2013

Do I really need to create a separate pointer to point to dynamic arrays?

T* temp = new T[capacity_ * 2];
T* tIter = &temp; //Do these need to be here?
T* dIter = &data; //Or can I use *(temp + i) and *(data + i)?
(for unsigned int i = 0; i < size_; i++) {
*(tIter + i) = *(dIter + i);
}

View 7 Replies View Related

C :: Passing Array Of Pointer To A Function

Sep 7, 2013

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CMD_LINE 500
void tokenize(char *cmd_ln, char *fun_tknzd[], int *argument_cnt);

[Code] ....

I am trying to pass the value of fun_tknzd to str_tknzd

View 4 Replies View Related

C++ :: Returning A Pointer To 2D Array From A Function

Dec 10, 2014

I know how to pass a 2-D array to a function. The prototype for that is void f(int (*p)[2]) assuming the array is of integers and there are 2 columns in it.

However, if I wanted the same function to return a pointer to a 2-D array, what would be the prototype?

View 2 Replies View Related

C++ :: Passing Array As Pointer To A Function?

Apr 24, 2014

I can clearly understand how to pass a dynamic array to a function and modified within the function..

My aim is to get back the array initialized as I am trying on the next sample of code..

Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <fstream>
using namespace std;
#define N3//number of bodies
void initialise( double *M[], double *X[], double *y[])

[code]....

View 14 Replies View Related

C++ :: Pointer Incrementing Using Dynamic Memory Allocation?

Feb 13, 2013

I am using a pair of pthreads that call a pair of functions for ping-pong dma data transfer that are used in a loop for data transfer from an acquisition board. For a large # of waveforms, I ultimately run out of PC memory and the program stops. At the end of each function I use the delete[] command to clear memory for reuse, but the pointer appears to advance by the array size used for the transfer until the location exceeds the 2 GB I have for memory. I can see this happening using the Task Manager performance button time plot and window of total memory used continuing to increase to the limit. The culprit for one of the functions (2nd) is:

unsigned char* dataBuffer2 = (unsigned char *) (pci_buffer2.UserAddr);

where pci_buffer1 and 2 have been set up and allocated in main. I also had the following line in each function process:

double* Rin = new double[length];

and it used up memory twice as fast. When I transferred the last line to an area just prior to main and used a constant 1024 for length, the program ran twice as far before exceeding system memory, so it appears that both lines were forcing new memory assignments and moving the pointers accordingly. In addition to using the delete[] command to free memory unsucessfuly at the end of each function procedure, I ended up closing the memory at the end of each procedure, then reallocating it again with the idea that the pointer would be set back to the original value, but it still seems to icrement along. So, neither approach appears to allow reuse of the memory because the pointer continues to march along. Using Visual C++ 6.0 to compile.

View 1 Replies View Related

C++ :: How To Send A Pointer To ONE Element In 2D Array To Function

Feb 2, 2015

I am trying to do this.

if (isInteger(&(tokens[i][j])) == true || isDouble(&(tokens[i][j])) == true)

Instead of sending the address of the element at row I and column j, it send the address of the entire row I. What is wrong?

View 8 Replies View Related







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