C++ :: Pointer To Array Of Pointers
Feb 10, 2013
I'm just trying to get a handle on the uses of pointers here. Though clearly from my errors I'm missing a key concept. Here is my code: (You can assume that the array, "array_size" has values in it, I did this part in another function)
int main() {
bool **ptr_array;
int num;
int *array_size;
cin>>num;
[Code] ....
Once the program reaches the word[num] = false; some unhandled exceptions pop up.
I simplified my code a bit from my actual program and mixed up the loops, now the code should be in its correct form.
View 4 Replies
ADVERTISEMENT
May 24, 2013
I am having following pointer variable declaration
Code: Static double (*funcs[]) (double) = { sin,cos,tan,asin,acos};
Here funcs is an array of pointers to functions which take double as input and gives double as output with static as storage type am I right.
View 2 Replies
View Related
Feb 28, 2014
I'm trying to create an array of pointers to pointers which will point to array of pointers (to strings) I tried
Code:
int i;
char *string[]={
"my name is dave",
"we like to dance together",
"sunny day",
"hello",
[code]...
the app keeps crashing , I don't know how to make the array-elements to point to another array-elements..
View 4 Replies
View Related
Jan 3, 2015
I am trying to make a pointer to pointers for strings with malloc but my code isn't right.I tried something like that.
Code:
char **p;
**p=malloc(100*sizeof(char*));
for(int j=0;j<100;j++)
*(p+j)=malloc(10*sizeof(char));
how could i allocate memory is only needed for every string(as long as it is)? Cause in my code i allocate memory for 100 strings and for 10 characters for every string.
View 1 Replies
View Related
Sep 14, 2014
i wrote a program using pointers in a basic stack program below is the pop and push functions and their calls in main. basically getting random numbers and converting to char characters and inserting them into an int array.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
[Code]....
View 9 Replies
View Related
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
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
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 18, 2014
I'm trying extremely hard to understand pointers and I have the basic concept down.. I feel as though my knowledge of dynamically allocated pointers and pointers in general is not enough to understand the logic behind what I'm trying to do. The problem is that the donations array must be able to accept any number of donations. I've made it do just that, but there is also an array of pointers which must each point to the same element in the donations array. The program works if I assign int *arrPtr[100] for example, but it does not work if I try to dynamically allocate it to accept the same number of elements for donations entered by the user. Here it's the snippet
#include <iostream>
using namespace std;
//Function Prototypes
[Code]....
View 2 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
May 21, 2014
I am trying to initialize an array of pointers to an array of characters, I can do it in 3 lines but I really want to do it in one line at the same time keeping the #define.
3 lines initialization (can compile)
======================
#define A 1
#define B 2
char row1[] = {A|B, B, A};
char row2[] = {B, A};
char *test[]= {row1, row2};
1 line initialization (failed)
===============================
char *test[] = { {A|B, B, A}, {B, A} }; // <- how do i do this??
I do not want this because it waste ROM space
=============================================
char test[][3] = { {A|B, B, A}, {B, A} };
View 18 Replies
View Related
May 10, 2013
I am working on an assignment identical to another post from a couple years ago, for reference here is the thread:
array of pointers to structures sorting addresses by zip code
They way it is written on that thread is almost identical to the way the teacher implied to have it done (only wrote part of the input block). But I am having an error:
When it gets to the output section it outputs then next name along with the zip code... I tried strncpy and strxfrm but both cause more problems than they did work.
The last part of the project is to have the output put out in order of least zip code to most zip code (00000<99999), so this is causing me a real problem and I do not see what exactly is making this happen.
Here is my code (we dont HAVE to use gets but professor suggested using it for this assignment, next lab is to rewrite this using files and fgets rather than I/O redirection):
header.h Code: #ifndef lab_6b_7b_Header_h
#define lab_6b_7b_Header_h
//header file intiating other headers
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
[code]....
I have not started the sorting code because I cannot get past this, but once I have proper zip codes I am sure I can make a sort function no problem.
I am using xcode with some breaks to read variables as various points and do not notice anything wrong until it makes it to the output functions, although this page briefly pops up between input and output functions when the breaks are up:
View 8 Replies
View Related
Mar 6, 2015
I'm wondering how to access Buffer 1 and 2 via the pointer array;
Code:
char *BufPtrs[3];
char Buffer1[64];
char Buffer2[64];
BufPtrs[0] = Buffer1;
BufPtrs[1] = Buffer2;
BufPtrs[2] = NULL;
I thought that if I were to access Buffer1 via BufPtrs[0], I would simply just put an * to it before printf()-ing or store it in a char[] (equivalent to a string).
View 2 Replies
View Related
Aug 9, 2013
How do I store pointers to a struct in an array ? I am using sprintf to concatenate some values together and then output it to an array in its 1st argument. A portion of my code is shown below.
Code:
int count = 0;
struct addx {
int a;
[Code].....
View 1 Replies
View Related
Sep 27, 2013
I've created an Array of pointers to objects using:
Person ** A = new person * [arraysize];
When I intend to access a specific person do I have to do this? :
something = A->[i];
and when I want a specific object within my struct do I have to do this? :
something_else = A->[i]->random_int;
View 10 Replies
View Related
Feb 14, 2013
I have an assignment where I need to use pointers to do a few things and I am a little confused on the syntax of it all. My question is how do you use a pointer to point to an array of structs.
For example
struct info{
char firstName[15];
char lastName[15];
};
main() {
info person[4];
cout << "The third letter of the second persons first name is: "; // ?????
}
how would I define a pointer to point to the third letter of first name the second person in the "person" array.
View 2 Replies
View Related
Dec 5, 2013
my code:
int OKCount=0;
int WaitingCount=0;
int ReservationCount=0;
Flight::Flight(int capacity, int waitingMax) {
seats=capacity;
[code].....
reservations is a data member in the class flight as:
Reservation **reservations;
OKReservation is a derived class and its abstract base class is Reservation.
My problem is that the reservations array loses its value in other function
View 8 Replies
View Related
Mar 6, 2015
What I'm trying to do with this code is an address book and I have an array of pointers which are returned by malloc whenever I need to add an extra entry in the address book. What I need to do is search for a specific entry using bsearch. I've got an inqSort() function that sorts the table and runs normally, but my program crashes when I try to use bsearch.
Code:
typedef struct {
char name[20];
char phone[14];
} abEntry;
[Code] ....
Every time an entry is inserted, I inqsort() the array so it's always sorted, and it works as expected. But when I try to call findEntryUI(); from the main() function, the program crashes after entering the name I want to search.
View 3 Replies
View Related
Mar 24, 2013
why when I print out "array[2]" nothing prints? It just prints blank space. My file definitely has text in it, but when I try to assign "text" into the array of pointers it won't show any text. I know fgets() appends a newline at the end of the string, not sure if that has anything to do with it, but I've tried printing everything that should be in "array" with a for loop and I get nothing.
Code:
#include <stdio.h>
#define FILEPATH "/home/user/Desktop/text"
int main(){
FILE *myFile = fopen(FILEPATH, "r");
if(myFile == NULL) return 0;
char text[100];
char *array[100];
int idx = 0;
while(fgets(text, 100, myFile)){
array[idx++] = text;
}
puts(array[2]);
}
View 2 Replies
View Related
Jun 11, 2013
I have a little problem with one of my functions. The function purpose is to get a number (n) and create an array (size n) with pointers to strings (each string length is 20 chars) and i don't know why but during the debugging i get a <bad ptr> message and this message :
CXX0030: Error: expression cannot be evaluated
This is my function:
Code:
char** getlist(int n) {
int i=0;
char **arr;
arr=(char**)malloc(sizeof(char)*n);
if (arr==NULL)
[Code] ....
View 8 Replies
View Related
Jan 29, 2015
I basically have some code that lets users register callbacks into a callback table at a specified index. There is one element in this table for each event that can trigger a callback. I basically do something like this:
In a header file
Code:
typedef struct _GMclient
{
GMcommunicator gcomm;
GMclient_callback callback_table[(int)MAX_CALLBACKS];
}GMclient;
typedef int (*GMclient_callback)(GMclient*, void*, void*);
Then I allow them to set the callback with a function that basically ends up doing this (in a .c file that includes the previous mentioned .h file):
Code:
void
GMclient_register_callback(GMclient *client,
GMclient_callback fxn,
int index)
[Code] ....
Then later when I need to invoke that callback, I pull it out of the table
Code: GMclient_callback *fxn = client->callback_table[CMD_INDEX];
However, I get compilation errors:
Code: src/gmanager_client.c:43:6:
error: a label can only be part of a statement and a declaration is not a statement...
View 4 Replies
View Related
Aug 4, 2013
How would I pass let say 2 array pointers to a function X , allocate memory for them in X , fill them with values and get them back in my main function without creating a structure.
example:
Code:
void X(int *a, int*b){
a= malloc ...
b = malloc ...
// fill a and b
return them back to the main function
}
void main(){
[Code]...
View 3 Replies
View Related
Feb 4, 2014
Code:
#include <stdio.h>
void Swap(int *x, int *y);
int *Largest(int *array, int size);
int main()
{
int a, b, i;
int c[10];
int maxaddress;
}
[code]...
My swap function works fine, but I am trying to find the ADDRESS of the largest element in my array and I am getting an error using gcc on my "return &largest;" and my printf line in my main function.How can I fix this and return the address of the largest?
View 8 Replies
View Related
Dec 8, 2013
I am trying to alphabetize 3 different strings by comparing the first letter in each one. It will work for the first two names, but when I try to print out the third, I keep getting errors and I don't know why?
Code:
#include <stdio.h>
char personName();
int main() {
personName();
return 0;
[Code] ....
View 4 Replies
View Related
Aug 8, 2013
I am having some trouble getting a 3d array of pointers to link up with a linked list for each of the array elements. the 3d array of pointers is declared like this:
Code:
struct particle *cell[MAXCELLS][MAXCELLS][MAXCELLS]; and there are linked lists declared like this:
Code: struct particle { /* structure for particles */
double sw[3]; /* square well components */
double hs[3]; /* hard sphere components */
double u[3]; /* unit vector for rotations */
struct particle *link;
};
I want to get the array pointers 'cell[x][y][z]' to point to the first observed particle in a function next to main, something like this:
Code:
void generate_list(){
int i,j,k,l;
/* determine the number of cells to decompose the simulation box in each of x, y and z directions*/
int(cells_x) = floor(boxX/cell_size);
int(cells_y) = floor(boxY/cell_size);
int(cells_z) = floor(boxZ/cell_size);
/* initialise the array of pointers to NULL */
for (j=0;j<cells_x;j++){
[Code]...
I am getting a pointer type cast error when I compile "assignment from incompatible pointer type",
View 6 Replies
View Related
Mar 30, 2013
I am able to work with n instances of a structure in one mallocated area, but when I try to do the same thing with just character pointers, I get compiler errors about making integer from pointer without a cast. If I create a structure with just a character pointer in it, it works just fine... I am just not seeing something here!!!
This works:
Code:
#include <stdio.h>
#include <stdlib.h>
int main (void) {
struct items {
unsigned int item_1;
unsigned int item_2;
[Code]...
This DOES NOT work!
Code:
#include <stdio.h>
#include <stdlib.h>
int main (void) {
char * items_ptr = NULL;
unsignedint i = 0;
char * one = "one";
char * two = "two";
[Code]...
View 9 Replies
View Related