C :: Access Element Of Array Through A Pointer To A Pointer
Dec 25, 2013
i have been fiddling with pointers but I don't understand how the proper syntax is written when I want to acces an element of an array through a pointer to a pointer...The code is all mostly just random bs for learning purposes. I marked the problem "// THIS LINE"
Code:
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#define MAX_DATA 100
int find_average(char *iden, ...) {
[Code]...
View 2 Replies
ADVERTISEMENT
May 13, 2013
How would I go about having a pointer to an array element specificity a character in a c-string.Every thing I try will not even build.An array is already a pointer to the first location of the array right?
char *pHead;
char *pTail;
pHead = sentence[0]; <=== This wont build
pHead = &sentence[0];
pHead = sentence[0]*;
*pHead = sentence[0]; <===== this builds but is not storing anything
View 5 Replies
View Related
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
Sep 26, 2013
question from chapter 11, qn 8 modern C programming by king
Write the following function: Code: int *find_largest(int a[], int n); When passed an array a of length n, the function will return a pointer to the array's largest element
Code:
#include <stdio.h>
int *find_largest(int a[], int n)
{
int i, x;
x = 0;
[Code].....
have traced the code line by line and it prints correctly up to line 31. However, when the program exits (line 35) , it goes to some other screen with very complex code and my output disappears.
Just to clarify: If I just run the code as is, there is no output. However, when I trace the code line by line, the output appears, but then disappears as the program exits My input was 1 2 3 4 5 6
View 11 Replies
View Related
Jul 16, 2013
This is a sample program that declares a Matrix as a structure with an array as a pointer to a pointer. The following program is supposed to store a matrix in the structure "_Matrix" and later print the matrix just entered but it fails giving me a "segmentation fault". The sample code is given below
Code:
#include <stdio.h>
#include <stdlib.h>
struct _Matrix {
int row_size;
int col_size;
int **mat;
[Code] ......
View 1 Replies
View Related
Jan 3, 2013
I have the following code :
Code:
#ifndef TDYNAMICARRAY_H
#define TDYNAMICARRAY_H
namespace Massive {
template<class T>
T **AllocateDynamic2DArray(int nRows,int nCols)
[Code] .....
I wish to know how to traverse or loop through a dynamic 2D array using pointer to pointer as returned by the code above. Like I would in a static T[20][20] 2D array.
View 8 Replies
View Related
Jan 10, 2015
I have the following scenario :
Code:
class test {
public :
int num;
};
int main() {
test t1;
test *ptr = &t1;
int test :: *mem_ptr = &test::num;
}
I need to access mem_ptr (pointer to a class member) through :
a. object (t1)
b. pointer to the object (ptr).
How can i do that ?
View 4 Replies
View Related
Jan 22, 2015
I need to know how to access the nested structure using pointer, i mean i know how to do that when i have simple structure, for example:
Code:
struct person{
char fname[16], lname[16];
int age;
}
for this example i can use
Code: (*pointer).age
or
Code: pointer->age
But if i have structure like this:
Code:
struct date{
int day, month, year;
}
struct person{
char fname[16], lname[16];
struct date birthDate;
}
Then how can i access the birthDate using pointer?
View 2 Replies
View Related
Jan 31, 2015
#include <stdio.h>
struct stack{
int n,,a[100];
}s;
int main()
[Code] ....
this program is not running why can't i access elements in this way directly.
View 1 Replies
View Related
Oct 1, 2013
I would like to access (use) a (virtual) method declared in a base class via a pointer to an object belonging to a derived class, which overrides the base method. Is it possible? Up to now I have not been successful. I am including a program with a more detailed description.
// declare a base class T0 with virtual function P, and a derived
// class T1 which overrides P; declare two ptrs, to T0 and T1,
// and generate corresponding objects; calling P via the two
// ptrs gives expected result; however I would like to take
// ptr to T1 object & execute the base function (declared
// in T0); is it possible? I tried all possible casts but nothing
// works; no matter how I cast the pt1 ptr, I always end up executing
// the overriding function (declared in T1)
// actual output (debian 7, gcc 4.7.2-5):
[code]....
View 5 Replies
View Related
Feb 17, 2014
This code works:
//void Fighter::LoadAnimation(...){
std::list<SCML_SFML::Entity*> entities;
for(std::map<int, SCML::Data::Entity*>::iterator e = data.entities.begin(); e != data.entities.end(); e++) {
SCML_SFML::Entity* entity = new SCML_SFML::Entity(&data, e->first);
entity->setFileSystem(&fs);
entity->setScreen(&window);
entities.push_back(entity);//problem line
}
}
But if i change entities to a member variable, std::list<SCML_SFML::Entity*> Fighter::m_entities, it does not. Instead i get a write access violation when i try to push_back(entity). i need it to be a member variable because i need to use it in other member functions.
View 3 Replies
View Related
Jan 17, 2014
I was having problems changing the value of my head node I passed it as an argument as head which would be the address. The parameter was defined as struct node *head. like this
bool deleteNode(struct node *head, struct node *delptr)
I tried manipultaing pointer values to change head node value but it did not work. I saw some code online which used pointer to pointers(in code below) to change head node value it worked I dont fully understand why. Would like better understanding of why.
Would also like to know why the argument call needed &head instead of just head.
remove = deleteNode(&head,found); opposed to remove = deleteNode(head,found);
#include "stdafx.h"
#include<iostream>
struct node{
[Code].....
View 1 Replies
View Related
Aug 19, 2014
I am attempting to implement function pointers and I am having a bit of a problem.
See the code example below; what I want to be able to do is call a function pointer from another pointer.
I'll admit that I may not be explaining this 100% correct but I am trying to implement the code inside the main function below.
class MainObject;
class SecondaryObject;
class SecondaryObject {
public:
[Code]....
View 10 Replies
View Related
Nov 27, 2013
I have to write a program to print pascals triangle and stores it in a pointer to a pointer , which I am not entirely sure how to do. I also have to write the file and read it, then create a binary file. Assignment is attached. I am not the best with programming and especially with pointers. I will post my code below.
Code:
#include <stdio.h>
#include <stdlib.h>
void writePascalTriangle(char *fileName, int heightOfTriangle, int **triangle) {
FILE *fp;
fp=fopen("writePascalTriangle.txt", "w");
[Code] ....
View 4 Replies
View Related
Apr 19, 2014
I'm making a system like twitter for class called ShoutOut.com I want to be able to get the PublicShoutOut pointer pointed to by the start iterator and assign it to firstShoutOutToDisplay and secondShoutOutToDisplay because I need that in order to pass the pointers to one of my functions. When I step through the debugger the values in start are all default values like "" and so are the values in this->firstShoutOutToDisplay but the message that start points to is being output just fine.
EDIT: got rid of irrelevant code. Am I using the correct syntax to do this?
if (start != finish) {
//getting these because a shoutout needs to be passed to the function that displays
//options for a shoutout
this->firstShoutoutToDisplay = (*start);
[Code] ....
View 2 Replies
View Related
Mar 7, 2013
I have a function:
const void insertStuff(const void *key, const int value){
// I want to convert the void pointer into one
// of three types of pointers(int, string, or double)
switch(value){
case 0:
int *intPtr = key;
[Code] .....
But this causes an error of: "crosses initialization of int*intPtr"
What's the correct way of implementing this?
View 1 Replies
View Related
Jun 20, 2014
i really don't know why has a error in my code, that pass a pointer of pointer (name of a matrix with 2 dimensions). Here is the source code of a simple example where appears segmentation fault when execute (but compiles normal):
#include <stdio.h>
#define LINHAS 3
#define COLUNAS 5
float a[LINHAS][COLUNAS];
void zeros(float **p,float m, float n){
int i,j;
for(i=0;i<m;i++)
[Code]...
View 6 Replies
View Related
Mar 4, 2015
I need to make a copy of a string that is defined by char *full and copy it into a different pointer defined by char *duplicate. I have written code to do this however it will not work and i cannot figure it out my code is as follows:
char *duplicate = (char *)malloc(strlen(full) + 1);
strcpy(duplicate, full); /*Make second version of full*/
char *Ptr = strtok(duplicate, " "); /*Split duplicate up*/
I have a full program written but i know this is where the problem is because i have used printf statements to see where the program fails. I get no errors and it compiles successfully but it hits this point of the program and it just stops and windows automatically shuts down the program.
char *full is pointing to:
"To be, or not to be? That is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles,"
I need to duplicate the string because i need to use strtok but i will need the original string later on so i need an unaltered version.
View 9 Replies
View Related
Aug 4, 2013
How to access an element of the point3f vector. E.g. :
int main() {
vector<Point3f> a;
a.push_back(Point3f(0,0,0);
a.push_back(Point3f(0,0,1);
//print first x,y,z element
cout<<a[0]<<endl;
[Code] ...
But it doesn't work firstly because it says can't use typename i.e. <float>
View 8 Replies
View Related
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
Feb 6, 2015
I create an instance of a base class (not derived class) and assign it to base class pointer. Then, I convert it to a pointer to a derived class and call methods on it.
why does it work, if there is a virtual table?
when will it fail?
// TestCastWin.cpp : Defines the entry point for the console application.//
#include "stdafx.h"
#include <iostream>
class B
{
public:
B(double x, double y) : x_(x), y_(y) {}
double x() const { return x_; }
[Code] ....
View 14 Replies
View Related
Jun 5, 2012
"
#include <stdio.h>
struct datastructure {
char character;
};
void function(struct datastructure** ptr);
[Code] ....
These codes give these errors:
error: request for member 'character' in '* ptr', which is of non-class type 'datastructure*'
error: request for member 'character' in '* ptr', which is of non-class type 'datastructure*'
These errors are related to
"
*ptr->character='a';
printf("Ptr: %c",*ptr->character);
"
I want to access "character" data inside the structure "trial" by a pointer to pointer "ptr" inside function "function",but I couldn't find a way to do this.
View 3 Replies
View Related
Dec 7, 2013
In my refference book I have got a example with a part saying to access the a[4][0] element of the array (named a) with pointer this can be written:
*((int*)a+4)
I wonder if the cast is really required. The book says it is required so that the pointer arithmetic can be done properly. However I am not sure about it. When I work with pointers defined by myself I don't use casts similar to this one. Is there a difference between a self defined pointer and array name?
View 14 Replies
View Related
Jun 20, 2013
I have written this code, and at first glance it does what I want, however I am worried that
a) I am overwriting the array that is apssed from chord.getPattern()
b) Im getting a memory leak that I want to get rid of, and
c) is there generally a /what is the neater way to do it:
Code:
uint8_t* ChordBuilder::invert(uint8_t count, Chord chord) {
temp = chord.getPattern();
chord.invert(true);
//TODO count is how many times to invert. Moves root aswell however
for (uint8_t i = 0; i < count; i++){
[Code] ....
temp is a member variable of ChordBuilder - and is expressed as: Code: uint8_t* temp; I dont want the pattern that chord stores, and passes with getPattern() to change - I fear it is at the moment?
I would rather not use the "new" but I cant think how to get rid of it, however Im not sure where I would need to put the "delete"?
View 7 Replies
View Related
Jul 31, 2014
What is the correct method of declaring pointer to multidimensional array
Code:
int array[3][4];
int *p = array;
The above code works but it shows warning. Is it an invalid way of using the pointer? the array is an address then why am i getting warning?
View 1 Replies
View Related
Oct 27, 2013
I have the following situation:
Code:
void myFun(float *pfMyPtr) {
float Val[] = {0.234, 0,432, 0.322, 0762, 0.984};
pfMyPtr = Val;
}
int main() {
float *pfPtr;
pfPtr = (float*) calloc (5,sizeof(float));
myFun(pfPtr);
}
I would like pfPtr to contain the values of array Val. What am I missing here?
View 5 Replies
View Related