C :: Functions Which Change Arrays Without Pointers?

Jan 17, 2014

the book I learn from gave a task to write a program which gets a matrix , and we have to write a function that switches 2 columns or rows the user inputs .as far as I know a function can not change variables in the main function without using pointers .so , theoretically, can a function described here can be written without using pointers ? as far as I tried - it can not.

View 9 Replies


ADVERTISEMENT

C++ :: Dynamic Creation Of Arrays Of Pointers To Arrays Of Pointers

Apr 15, 2013

I'm trying to write a function that takes a 32bit address and a data to store at this address.

I'm wanting to take the 32 bit memory address eg 0x12345678 and split it
into 4 x 2 bytes
12, 34, 56, 78

then each of the 4 entries is at most a 256 entry array.eg
FF, FF, FF, FF

So in this example, 0x12 points to 0x34 in the second array, which points to 0x56 in the third array, which finally points to 0x78 in the last array. This last array holds the actual data.

After successfully doing 0x12345678, say I might get a read for 0x1234AABB. So, the first and second pointers already exist, but I then have to create and write to dynamically created arrays.

The arrays need to have all entries set to NULL so that i know whether to follow the pointers to overwrite a previously entered value or create new arrays and pointers.

It all looks good and simple in the pseudo code I've written up but I'm having trouble coding it. I'm currently trying to deal with the first entry case, ie all array elements are NULL, but I'm getting confused with the pointers and creation of new arrays.

void cpu::store(unsigned int mem_add,unsigned int mem_val) {
int first = (mem_address&4278190080)>>24;
int second = (mem_address&16711680)>>16;
int third = (mem_address&65280)>>8;
int fourth= (mem_address&255);

[Code] .....

A1 has been declared as
int* A1[256] ;

View 3 Replies View Related

C++ :: Using Pointers To Change Some Value On The Fly?

May 13, 2015

I've got an interface that provides the methods that are to be implemented

Code:
class A {
virtual void A() = 0;
}
class B : public A {
std::string operator<<(const std::string& lhs);

[Code] ....

I don't know why the << operator in B is never called..

View 5 Replies View Related

C :: Linked List - Change Struct Pointers

Dec 17, 2013

I have a linked list where each node contains a pointer to a string, I'm treating the linked list as a text file and each node/string contains each line in the text file.

I have all of that working, but now I need to create a switchLine function that takes in the two line numbers and I need to switch those lines in the text file. I initially thought of switching the pointers to each the next node in the linked list such that the node order changes, but I then realized how much work that would actually take when you need to consider so many cases. So I instead thought of changing the pointers to strings. If I want to switch the 2nd and 3rd line, then I'd want to have the 2nd node in the list have its char * pointer point to the memory position that the 3rd node's char pointer points to, and vice versa.

View 2 Replies View Related

C :: Using Functions To Convert Change Into Dollars

Jul 20, 2013

I know this is pretty basic script the problem I have is that I have to use functions for each calculation. The problem I face is that when the user types in the amount of quarters, dimes, nickels, and pennies when a function tries to add them together it displays a huge number instead of the right number.

Here is my code:

#include <stdio.h>
void insert_money(int p, int n, int d, int q, int total){
printf("How much of each coin do you want to insert:
");
printf("Quarters: ");
scanf("%d", &q);

[Code] ....

View 7 Replies View Related

C++ :: Cannot Change Virtual Functions From Object?

Oct 14, 2013

I try the functions pointers too, but without success. I understand the objects are the way for work with class's. until here fine. But why i can't change the virtual functions from an object? is there anyway for do it? Ican't do, outside of classfunctions, these code:

#include <iostream>
using namespace std;
class test {
public:
virtual void created(){};

[Code] ....

How i can validate these line:

void a::created()
???

View 2 Replies View Related

C :: Passing Pointers To Functions

Feb 28, 2015

I have been struggling with pointers. I am trying to write a program that first asks a user to input a filename. It then checks if the file exists and if it does it passes a pointer to the next function. The next function then asks the user for a specific word to look for and the function will search a text file for the word and do some other operations. My problem is that I do not understand how to use the pointer returned by my first function as an input to another function.

The following code has the first function file_check() and the second function word_search() which I think the way I am declaring it is the problem.

Code:
FILE *file_check();
void word_search(FILE *);
int
main(void) {
FILE* check= file_check();
// word_search(check);

[Code] ......

View 6 Replies View Related

C :: Pointers As Arguments In Functions

Aug 23, 2013

I'm having some problems with a function. The function is supposed to find the two largest values in an array.

Code:

void find_two_largest( const int *a, int n, int *largest, int *second_largest){
largest = a;
int temp;
second_largest = a;
for ( int i = 1; i < n; i++){
if (*(a + i) > *largest){
temp = *largest;

[Code]....

I don't see any mistake with the code of the function, but when I try to call it inside my program it only returns 0 for both largest and second_largest.

Code:

int *find_middle( int *a, int n);
void find_two_largest(const int *a, int n, int *largest, int *second_largest);
int main()
{
int n;

[Code]...

Do I have to declare the variables largest and second_largest as normal integer variables and then pass their addresses as arguments to find_largest or is that incorrect?

View 10 Replies View Related

C :: Getting Null Pointers In Most Of Functions

Oct 15, 2014

For some reason I keep getting null pointers in most of my functions, and in my convertTime function the numbers are way higher than they should be.

Code:

//Utils.H header file----------------------------------------------

//This function takes the radius of a circle and returns the diameter, the circumference and the area.
int circleStatistics(double radius, double *diameter, double *circumference, double *area);

//This function takes a number of days and returns how many years, weeks and remaining days that is.
int convertTime(int days, int *y, int *w, int *d);

//This function takes a length of time and calculates the dilation of that time at a percentage of the speed of light.
int lorentzTimeDilation(double normalTime, double percentC, double *dilatedTime);

[Code] ....

View 3 Replies View Related

C/C++ :: Pointers Passed Through Functions

Dec 14, 2014

I am writing a code that creates a deck of cards using a doubly linked list. One function, newDeck(), is made to create a new deck if the user wants. I don't have any problems creating the deck and it all seems to work fine, but when I run the whole program and a separate function needs to utilize the nodes in my deck, I get an error. Trying to find where there was a problem, I tried displaying the values of a card node in random parts of my code. At the end of the newDeck function, after the whole deck has been created, the card still displays correctly, yet when I return back to main immediately after newDeck has been called, I noticed that the values of my cards changed to either random values or they became null. I found this strange because in between the end of my newDeck function and at this point, there is no extra code so it doesn't seem as if there is any way the pointers could have been changed. Maybe It's because I don't have a complete understanding of pointers yet but is there any way that pointers can change values when returning from a function to main?

newDeck(head, tail, n); //this is how I call the function from main.
void newDeck(Card* head, Card* tail, Card* n) //this is my function body
{
for (int i = 0; i < 4; i++)
{

[Code]....

View 2 Replies View Related

C :: Change Arrays In Function Int Palin To Pointer

Oct 15, 2014

I'm trying to write a program to test if a string is palindromic using only pointers

Code:

#include <stdio.h>
#include <string.h>
void revstr(char* str)
}

[code]....

I need to change the arrays in the function int palin to pointers.

View 4 Replies View Related

C++ :: Use Different Pointers For Arrays?

Dec 24, 2013

So when if you want to change the values themselves for example in an array (just for example) this is valid;

void test(int* test)
{
test[0] += 10;
}
int bob[] {1, 3, 5};

If you did that bob[0] would not equal 11. All well and good right?

Now if you do this?

int sally = 33;
test(sallay);

This wouldn't work at all you actually have to use

void test(int& test)
{
test += 10;
}

how the memory addresses etc. are working here? I don't understand why you need to use & the reference operator if it's not an array? Why wouldn't that still work?

View 3 Replies View Related

C :: Structs - Implement In With Pointers And Functions

Feb 23, 2013

I have a struct and I want to implement in with pointers and functions.

What is the corect syntax? For example:

Code:
typedef struct XYZ
{

int x;
int y;
int z;
}XYZ_t;

int func( using the XYZ_t struct)

[Code] .....

View 5 Replies View Related

C++ :: Calling Functions - Vector Of Pointers

Mar 27, 2013

I have a vector of pointers inside a seperate Exam class.

vector <Question* > question_list

The Question class is my base class in which I have derived sub classes for the different types of questions (MultipleChoice, LongAnswer, etc.). I am using my vector to hold the different types of questions.

in each of those classes I have virtual "write" functions in both the base and the derived classes, that write to a file differing for each type of question.

My problem now is calling the write function from a Exam function. I've tried several methods, such as:

for (size_t i = 0; i < question_list.size(); i++) {
question_list[i].write(testfile.c_str());
}

but it comes with two errors: "error C2228:left of '.write' must have class/struct/union" along with "IntelliSense: expression must have class type"

I have made a write function for the exam class as well but am not sure what it should include since the Exam class is not a derived class of the Question class.

View 15 Replies View Related

C/C++ :: LNK1119 And LNK1120 Errors When Trying To Change From Arrays To Struct

Mar 12, 2014

Im trying to take a previous project that was made with arrays and change it to a struct for storage.

Error2error LNK1120: 1 unresolved externalse:my documentsvisual studio 2013ProjectsConsoleApplication15DebugConsoleApplication15.exe11ConsoleApplication15
Error1error LNK2019: unresolved external symbol "void __cdecl getStudentInfo(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * const,int (* const)[5],float * const,char * const)" (?getStudentInfo@@YAXQAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAY04HQAMQAD@Z) referenced in function _maine:my documentsvisual studio 2013ProjectsConsoleApplication15ConsoleApplication15Source.objConsoleApplication15

I've been trying to research this error message but I haven't been able to find much that pertains to the simple code I have.

Below it the first part of my c++ program that includes everything that I have changed before the error appeared. It seems to be centered around changing the void function at the end

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <climits>
using namespace std;
const int NUM_STUDENTS = 10;
const int NUM_SCORES = 5;
void getStudentInfo(string[], int[][NUM_SCORES], float[], char[]);

[code].....

The final line was originally

void getStudentInfo(string sn[], int ss[][NUM_SCORES], float sa[], char sg[]) {

View 2 Replies View Related

C :: How To Use Character Pointers Instead Of Arrays

Apr 14, 2013

Here is my program to look up words. How do I use character pointers instead of using arrays?

Code:
#include <stdio.h>
struct entry
{

[Code].....

View 2 Replies View Related

C :: Dynamic Arrays And Pointers

Feb 11, 2013

I am having troubles with dynamic arrays and pointers. All the errors are of that kind. I think when I am assigning malloc to an array we assign to its address.

Code:
/*Create an array of genes of the large matrix*/
gene_t gene,gene1,gene2;
gene=malloc(INITIAL*sizeof(gene2)); Code:
while(...) {
if (gene_num==current_size){

[Code] .....

View 9 Replies View Related

C :: Arrays And Pointers In A Loop

Jun 27, 2013

Set an Array (a[10]) and a Pointer to that array (*pa) and code a loop (for( ; ; loop) that will advance that pointer (*pa) and will set a new content into it with each loop. that means that in the end of the day, my program will automatically set content to each cell of the array by promoting the pointer by 1 and add the sum to that pointer.

When I run the program it prints the address of the cells instead the value of it.

Code:
#include <stdio.h>
void main() {
float a[11], *pa; // Array and ptr set.
int i; // counter for the loop.
pa = a;
for (i=0 ; i<=10 ; i++) // the loop itself.

[Code] .....

View 3 Replies View Related

C :: Merging Arrays Using Pointers

Jul 17, 2013

Merging two arrays together using pointers!

View 4 Replies View Related

C :: Accessing Arrays Using Pointers

Aug 30, 2013

I came across the below code snippet in a project and was not sure how value of variable "response" is computed. Here as we can see, pic_data holds two one dimensional arrays but "response" access both the single dimensional array as two dimensional array.

Code:

#define MAX 100
#define MAXBUF 100
u32 response;
u32 index;
}

[code]....

View 3 Replies View Related

C :: Pointers And Bi-dimensional Arrays

Feb 6, 2013

I have a problem with a pointer and a bidimensional array. Here's the code:

typedef char t_board[5][4];
int i,j;
t_board *copy (t_board b) {
t_board *ptr;

ptr = (t_board *) malloc (sizeof(t_board));

[Code] ...

After this, I print new_board, and its ok; but the following code after the call to copy, crashes. Don't know why. if I remove the statement new_board = copy(some_board); , the code that follows runs ok.

View 4 Replies View Related

C++ :: Arrays And Smart Pointers

Aug 3, 2014

I'm writing quite a large program that needs to work with very large arrays (up to 100 million integers). It is necessary that i can access every point of the array directly (without running through the array) and that I use as little memory as possible. I first wrote the program with pointers that point to allocated heap memory. It works fine but now I wanted to use smart pointers instead (so I'm sure to have no memory leaks). Here's a simple visualization of my problem:

#include <iostream>
#include <memory>
using namespace std;
unique_ptr<int[]> upArray;
int main() {
int nArrayLength = 10;

[Code] ....

There are 2 things that do not work how I would like the to:

1. It wants me to assign the heap memory in one step: unique_ptr<int[]> upArray(new int[nArrayLength]); But I'd like to have the unique_ptr in my Class_Declaration before I know the array length and allocate the memory later.
2. *(upArray + i) = i;
cout << *(upArray + i);

Those lines don't work! How else can I do it.

View 6 Replies View Related

C/C++ :: How To Use Pointers To Arrays In Function

Feb 25, 2015

I am trying to use pointers to arrays in my function.

I can get the pointers to work outside of a function but I just can't figure out how to make them work in my function.jwhittle58, on 25 February 2015 - 06:06 PM, said:

I am trying to use pointers to arrays in my function. I can get the pointers to work outside of a function but I just can't figure out how to make them work in my function.

View 8 Replies View Related

C :: Using Typedef Struct And Passing Pointers To Functions

Mar 3, 2013

I am having problems with passing my values through functions.

Here is my code:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void test(int**);
void test2(int**);

[Code]....

View 8 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 :: Passing Arrays To Other Data Has Pointers?

May 7, 2013

I am looking for an example of when passing arrays to other data that has pointers,

I understand the terms I would just like to see a small example of code that actually demonstrates this process.

View 4 Replies View Related







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