pointers

2D Arrays and Pointers - C

Hi there, Just trying to really get my head round Arrays and Pointers in C and the differences between them and am having some trouble with 2d arrays. For the normal 1D array this is what I have learned: char arr[] = "String constant"; creates an array of chars and the variable arr will always represent the memory created when it wa...

Why am I getting a segmentation fault?

Hey guys I'm trying to write a program that takes in a plaintext file as it's argument and parses through it, adding all the numbers together and then print out the sum. The following is my code: #include <stdio.h> #include <stdlib.h> #include <ctype.h> static int sumNumbers(char filename[]) { int sum = 0; FILE *file = fopen(fi...

Modified a constant in c

const int z = 420; printf("\n%d | %d",z ,*(&(*(&z+1))-1) ); // O/P:420 | 420 printf("\n%u | %u",&z,(&(*(&z+1))-1) ); //address // O/P:1310548 | 1310548 *((char *)&z+1) = 21; //I change value for the 1st-Bit //corrupting constant printf("\n%d | %d",z ,*(&(*(&z+1))-1) ); //the com...

modified a constant again.. with void pointers

Possible Duplicate: Modified a constant in c const int z = 420; const void *v; v = &z; printf("\n%d | %d",z,*(int *)v); //420 | 420 printf("\n%d | %d",*(char *)&z,*(char *)v); //0th-Bit same value //-92 | -92 printf("\n%d | %d",*((char *)&z+1),*((char *)v+1) ); //1st-Bit same value //1 | 1 /*************************...

c++ how to make two vectors one with data the other points and reads only

In C++ I have 2 STL vectors A and V. A has data and is able to change it, V only points to data but reads only and can't modify it. So if these two vectors are inside a class what will be the syntax of Variables definition assigning A reference into V get_A() and get_V() will it return a reference or a pointer? Also If I have other...

If I can take a normal pointer to a variable within a class, what's the point of class member pointers?

Take this code: struct mystruct { int var; mystruct() : var(0) {} }; int main() { mystruct ins; int* p = &ins.var; *p = 1; } So what are some concrete really good examples of uses for the class member pointer? int X::*p = &X::data; /* p contains offset */ X object; X *objptr = new X; int i = o...

c++ convert matrix into row pointer vector

vector<vector<int> > mymatrix; vector<int> *ptr_vec; How do I make the ptr_vec point to the vectors which are inside mymatrix one after another. In more details Let's say mymatrix.at(0).size() gives 10, and mymatrix.at(1).size() gives 14 ..if I go ptr_vec->at(15) it should give me the fifth member in mymatrix.at(1) hope it doesn't conf...

c programming language,pointer

#include<stdio.h> #include<conio.h> main() { char *q[]={"black","white","red"}; printf("%s",*q+3); getch(); return 0; } Code gives output "ck". In this I want to know how *q+3 expression is evaluated. Means first *q is evaluated then 3 is added to what *q points to. In case of integer array it is simple to realise but h...

Pointers getting mixed up in android

I'm using ant to compile my android projects into debug apks. But sometimes when running my app, objects are getting mixed up and showing up at the wrong places. For example: <TextView android:id="@+id/deviceText" android:textSize="22sp" android:textColor="@color/white" style="@style/shadow" android:layo...

strcpy() and arrays of strings

I need to store the input from a user into an array of strings. #include <stdlib.h> #include <stdio.h> #include <string.h> char *history[10] = {0}; int main (void) { char input[256]; input = "input"; strcpy(history[0], input); return (EXIT_SUCCESS); } Running it on the terminal I get a Segmentation Fault and in ...

Value of a pointer in c

What are the values of char a,b; b="this is a character"; a=&b What will be the value of ***a, **a, and *a? How? Are there any good examples for the above? ...

Declaring a pointer to multidimensional array: C++

I've tried looking but I haven't found anything with a definitive answer. I know my problem can't be that hard. Maybe it's just that I'm tired.. Basically, I want to declare a pointer to a 2 dimensional array. I want to do it this way because eventually I will have to resize the array. I have done the following successfully with a 1D ar...

3D array C++ using int [] operator

hi, I'm new to C/C++ and I've been cracking my head but still got no idea how to make an "structure" like this It's supposed to be a 3D dynamic array using pointers. I started like this, but got stuck there int x=5,y=4,z=3; int ***sec=new int **[x]; It would be enough to know how to make it for a static size of y and z; Plea...

Boost::Scoped_Ptr breaks code

Examine the following code: This works: T *p = (std::find( this->first(), this->last(), *pPos )); if( p != last() ) { this->push_back(data); T *right = (this->last() - 1); T *left = (this->last() - 2); while( *pPos != data ) std::iter_swap( left--, right-- ); return const_cast<T*>(pPos); } This does n...

C++ vector of pointers problem

I'm currently trying to implement the A* pathfinding algorithm using C++. I'm having some problems with pointers... I usually find a way to avoid using them but now I guess I have to use them. So let's say I have a "node" class(not related to A*) implemented like this: class Node { public: int x; Node *parent; Node(int _x...

How do i store a set of values in an array without using square brackets, but by using pointers

In C programming, how can a store a set of values entered by the user into an array using only pointers and no square brackets? ...

Confusion about pointers and multidimensional arrays.

If the following is possible: MyFunction(int *array, int size) { for(int i=0 ; i<size ; i++) { printf(“%d”, array[i]); } } main() { int array[6] = {0, 1, 2, 3, 4, 5}; MyFunction(array, 6); } Why the following is not? MyFunction(int **array, int row, int col) { for(int i=0 ; i<row ; i++) { ...

Predict the number

I want the user to enter a 4 digit number and the program must tell what that 4 digit number was i.e generate that 4 digit number by Brute force attack.But at the line mentioned below the compiler says invalid indirection.I would also like to have some comments about they way I am implementing it,is it a good practise? #include<stdio.h>...

Returning a pointer from a member fucntion in C++

Hello I have a class with a function that returns a pointer: int* Maze::GetStart() { int startNode = 1; return &startNode; } Will the value of startNode be erased after the function returns the pointer? If I did this instead would the value be saved? int Maze::GetStart() { int startNode = 1; return startNode ; } m...

Functional Code Breaks When Used Twice.

I'm still working on my Field class, and tried to improve my piss-poor insertion/erase performance. However, the new function works once, then breaks catastrophically when I use it a second time. This is the code: template <class T> T *Field<T>::insert(const T *pPos, const T& data) { // Special case: field is empty. insert shoul...