pointers

give feedback on this pointer program

This is relatively simple program. But I want to get some feedback about how I can improve this program (if any), for example, unnecessary statements? #include<iostream> #include<fstream> using namespace std; double Average(double*,int); int main() { ifstream inFile("data2.txt"); const int SIZE = 4; double *array = new ...

What are pointers to class members used for?

I have read about pointers to class members, but I have never seen them being used in any practical applications. Can someone explain what are the use cases of such pointers? Is it really necessary to have such pointers? Eg. class abc { public: int a; abc(int val) { a = val; } }; int main() { int abc::*data; abc obj(5)...

Problem with pointers

OK, i have a strange problem. I have this piece of code: int *p; int test; p=&test; In Visual C++ express, in my exsisting project, I get this error: missing type specifier - int assumed. 'p' : 'int' differs in levels of indirection from 'char *' 'initializing' : cannot convert from 'char *' to 'int' But when i create new project, ...

At What point should you understand References?

I asked a question like this in an interview for a entry level programmer: var instance1 = new MyObject{Value = "hello"} var instance2 = instance1; instance1.Value = "bye"; Console.WriteLine(instance1.Value); Console.WriteLine(instance2.Value); The applicant responded with "hello", "bye" as the output. Some of my co-workers said th...

C++: why a self pointer of a struct automatically changes to void*

struct ptr{ int node; ptr *next; ptr(){} ptr(int _node, ptr *_next){ node=_node; next=_next; } }; struct list_t{ ptr *sht; int size; void push(int node){ size++; sht=new ptr(node,sht); } }shthead[100001], comp[200001], tree[200001]; The struct ptr is a smart pointer, be used as a linked l...

NullPointerException

Why do I get exception called NullPointerException if in Java there is no such concept as a pointer? Thanks ...

How to save pointer in C

Hi In my app, for debugging I want to save a pointer, before I do other operations on it, e.g. void foo(...) { /* suppose ptr1 points to one of my structs */ ptr1 = NULL; /* before that ptr1=NULL I want to save value of that pointer - how to do it ? */ } Thanks for any help ...

Fatal error when using FILE* in Windows from DLL

Hi there. Recently, I found a problem with Visual C++ 2008 compiler, but using minor hack avoid it. Currently, I cannot use the same hack, but problem exists as in 2008 as in 2010 (Express). So, I've prepared for you 2 simple C file: one for DLL, one for program: DLL (file-dll.c): #include <stdio.h> __declspec(dllexport) void print_t...

How do you delete a pointer without deleting the data the pointer points to?

I have a pointer that points to an array and another pointer referencing the same array. How do i delete any one of those pointers without killing the array such that the second undeleted pointer still works? for example: int* pointer1 = new int [1000]; int* pointer2; pointer2 = pointer1; Now i want to get rid of pointer1, how would ...

C newbie malloc question

Why doesn't this print 5? void writeValue(int* value) { value = malloc(sizeof(int)); *value = 5; } int main(int argc, char * argv) { int* value = NULL; writeValue(value); printf("value = %d\n", *value); // error trying to access 0x00000000 } and how can I modify this so it would work while still using a pointer a...

X11: How do I REALLY grab the mouse pointer?

I've implemented a horizontal splitter widget in Xlib. I'm trying to grab the mouse when the user clicks & drags on the splitter bar (so that the user can dynamically move the split & thus resize the windows on either side of the splitter bar). I've used XGrabPointer() after receiving a left click, in hopes that all future mouse motion...

Simple dynamic memory allocation bug.

I'm sure you (pros) can identify the bug's' in my code, I also would appreciate any other comments on my code. BTW, the code crashes after I run it. #include <stdlib.h> #include <stdio.h> #include <stdbool.h> typedef struct { int x; int y; } Location; typedef struct { bool walkable; unsigned char walked; // number of...

How does dereferencing of a function pointer happen?

Why and how does dereferencing a function pointer just "do nothing"? This is what I am talking about: #include<stdio.h> void hello() { printf("hello"); } int main(void) { (*****hello)(); } From a comment over here: function pointers dereference just fine, but the resulting function designator will be immediately c...

How do I properly turn a const char* returned from a function into a const char** in C?

In short, I would like to do this: const char **stringPtr = &getString(); However, I understand that you can't & on rvalues. So I'm stuck with this: const char *string = getString(); const char **stringPtr = &string; I can live with two lines. Am I introducing problems with this hack? I should have no fear of passing stringPtr out ...

How to use pointers and pointer aritmetic

: error C2064: term does not evaluate to a function taking 1 arguments : error C2227: left of '->name' must point to class/struct/union/generic type how do i fix this so this error doesn't happen for(int index = 0; index < (numStudents); index++) { if (student(index + 1)->score >= 90 ) student(index + 1)->grade = 'A'; ...

How to get Ponter/Reference semantics in Scala.

In C++ I would just take a pointer (or reference) to arr[idx]. In Scala I find myself creating this class to emulate a pointer semantic. class SetTo (val arr : Array[Double], val idx : Int) { def apply (d : Double) { arr(idx) = d } } Isn't there a simpler way? Doesn't Array class have a method to return some kind of reference to a p...

a nicer way to create structs in a loop

Hi guys, I haven't coded in C++ in ages. And recently, I'm trying to work on something involving structs. Like this typedef struct{ int x; int y; } Point; Then in a loop, I'm trying to create new structs and put pointers to them them in a list. Point* p; int i, j; while (condition){ // compute values for i and j with som...

Addition of an integer to a pointer

In following code, #include<stdio.h> int main() { short a[2]={5,10}; short *p=&a[1]; short *dp=&p; printf("%p\n",p); printf("%p\n",p+1); printf("%p\n",dp); printf("%p\n",dp+1); } Now the output I got was : 0xbfb45e0a 0xbfb45e0c 0xbfb45e04 0xbfb45e06 Here I understood p and p+1, but when we do dp...

Having issues with initializing character array

Ok, this is for homework about hashtables, but this is the simple stuff I thought I was able to do from earlier classes, and I'm tearing my hair out. The professor is not being responsive enough, so I thought I'd try here. We have a hashtable of stock objects.The stock objects are created like so: stock("IBM", "International Business M...

C++ vector of strings, pointers to functions, and the resulting frustration.

So I am a first year computer science student, for on of my final projects, I need to write a program that takes a vector of strings, and applies various functions to these. Unfortunately, I am really confused on how to use pointer to pass the vector from function to function. Below is some sample code to give an idea of what I am talk...