pointers

Is pointer better than instance when declaring members in class?

Option 1: class B{//}; class A { public: void Funcs(); private: std::vector<A> vecA; }; Option2: class B{//}; class A { public: void Funcs(); private: std::vector<A*> vecpA; }; Which one is better, is there any guidelines? ...

c++ vector of iterators

I understand that I can point to number of vectors std::vector<int> using for loop on one vector<int*> onev_point_2_all etc.. but how do i do that using iterators is there a way of creating a vector of iterators instead of a vector of pointers ? ...

change of vectors first pointer

hello, when i use vector to store some data, i usually access to this data by the pointer of the vector's first entry. because it it faster than the at() method. but i realize that when i insert a block of data, say an array to the end of vector, the first entry's pointer changes. this may be realated to stack stuff, but if i add the ar...

Perhaps a pointer problem. Linux program + libpcap + getopt_long() + C language.

Hello! I'm making a network sniffer for my college project using the libpcap library. Majority of the code works without any problem, however, I'm stuck with the following issue. I've added five command line options using getopt_long() function, but one option doesn't work as expected. The option is -d (--device_info) and is used to pri...

Take Address of Managed Structure

Hi all, (I am coding in C#) I want to pass a pointer to structure through an API. Say structure is, [StructLayout(LayoutKind.Sequential,CharSet= CharSet.Ansi)] public struct PassThisStruct { public Int32 Param1; public Int32 Param2; [MarshalAs(UnmanagedType.ByValArray, SizeConst=13)] ...

Return an object by reference

I have a problem returning an object that was created in other thread. The situation is this, my app need to send some request to remote server and wait an answer. In the main thread (1) i have a method called SendAndWait. This method put a message in a message queue for be sent and wait for an answer. Another thread (2) send the message...

c++ class pointer deletion segfaulting

I've got a simple class called object that I'm having a problem with. Theres one method which causes a segfault if I call it. I don't understand why. typedef class object { private: short id; std::string name; SDL_Rect offset; public: object(); object(short i, std::string n); ~object(); o...

Accessing/modifying an array of strings in a structure

Suppose I have the following code: typedef struct { char **p; } STRUCT; int main() { STRUCT s; *(s.p) = "hello"; printf("%s\n", *(s.p)); return 0; } which obviously doesn't work, but it should show what I want to do. How would I go about initialising, accessing, printing, etc the array of strings in the structure...

Tutorial on C pointers

Hi, right now I'm trying to teach someone about C programming. Specifically right now we're on pointers in C. However I feel this is a difficult concept to explain, as the person I'm teaching doesn't have much programming experience. I was wondering if anybody knew of a good tutorial on C pointers for a true beginner programmer? I've tr...

Can we avoid npe while checking for null in nested java objects?

1) if(null != parentObj.childObj) 2) if(parentObj.childObj != null) Do you think that "1" will avoid a potential null pointer exception in the case where 'parentObj' is null, in contrast to "2"? ...

Question on reuse of pointers after passing to functions

So, when I pass a const char * to a function once, can I use it again? It appears to end up spitting out crap to me. const char *config_file = "file.txt"; function(int x, config_file); cout << "Number" << x; secondfunction(int y, config_file); Do I need to make another pointer to config_file? If so, how do I do that? Thanks! ...

Changing pointer address in function

What do I need to change here so that animal contains {3,4}? void funct(unsigned char *elf) { unsigned char fish[2]={3,4}; elf=fish; } int main() { unsigned char animal[2]={1,2}; funct(animal); return 0; } EDIT: I see memcpy is an option. Is there another way just manipulating pointers? ...

in C what is the proper way to pass a pointer to a string from a function to the main.

this is the rough idea of what I am trying to do: I want the pointer in main to point to the word I just in my function. my actual code is very long so please excuse this format. main() { char *word; int lim 256; *word = function(word,lim)//I am not returning the address back only the first letter } function(word,lim) { //memory allo...

Reading a file to char array then malloc size. (C)

Hey, so lets say I get a file as the first command line argument. int main(int argc, char** argv) { unsigned char* fileArray; FILE* file1 = fopen(argv[1], "r"); } Now how can I go about reading that file, char by char, into the char* fileArray? Basically how can I convert a FILE* to a char* before I know how big I need to ma...

Access 2d array with point in wpf C#

Hi, I have a 2d array of a class. The size of array is very large (around 3000*3000) and accessing the array with ordinary row and column method is taking very much time. For this purpose, I want to use pointers to access the array. Following is my array code: Class definition: Class BoxData { Size _bound; bool _isFilled=fa...

How to initialise a pointer to pointer struct in C?

I have a struct which is a node, and another which is a list of these nodes. In the list struct, its an array of nodes, but instead of an array, it's a pointer to pointer with a size integer: typedef struct node { struct node *next; MyDef *entry; } Node; typedef struct list { Node **table; int size; } List; List *init...

Why does this malloc not work in C?

Just trying to make a kind of hash table with each node being a linked list. Having trouble just initializing the space, what am I doing wrong? #include <stdlib.h> typedef struct entry { struct entry *next; void *theData; } Entry; typedef struct HashTable { Entry **table; int size; } HashTable; int main(){ HashTable *ml; ml =...

problem in taking input for the char type in c.

hello i am having a trouble in taking input for the character type in c.the behavior of my source code is unusual. my code is: int n,i; char *ps; printf("Total no:"); scanf("%d",&n); ps=(char *)calloc(n,sizeof(char)); for(i=0;i<n;i++) { printf("Enter character %d:",i+1); scanf("%c",ps+i); } then as per my requirement it...

Book for pointers in C

Possible Duplicates: Where can I learn more about pointers? The Definitive C Book Guide and List Can anyone suggest me a book for pointers in C?I have many of the books but pointers is something which I still think I lack. ...

What does Far mean in c?

const struct sockaddr FAR* name, ...