pointers

Is the following C++ code equiv? (in a smart pointer implementation)

Code 1: template<class T> const PtrInterface<T>* PtrInterface<T>::newRef() const { PtrInterface<T>* me = (PtrInterface<T>*) this; ++me->references_; //++this->references_; return this; } Code 2: template<class T> const PtrInterface<T>* PtrInterface<T>::newRef() const { //PtrInterface<T>* me = (PtrInterface<T>*) this; //+...

Take substring of a string in c.

I want to take a substring of the string buffer by doing something like the below. I don't know if it's possible (I've been coding in C for all of about 6 hrs now, but feel free to be as technical as you like, I think I can handle it (though I may be wrong)) Edit: I want to take a substring of buffer from the beginning of buffer to the...

Confusing pointers in C

I have more than one doubt so please bear with me. Can someone tell me why this code fails? #include<stdio.h> void main(int argc,char **argv) /*assume program called with arguments aaa bbb ccc*/ { char **list={"aaa","bbb","ccc"}; printf("%s",argv[1]);/*prints aaa*/ printf("%s",list[1]); /*fails*/ } I assumed it had somet...

C++: Will structure be copied properly?

Hello, I have a pointer to a structure and I need to implement a method that will copy all of the memory contents of a structure. Generally speaking I need to perform a deep copy of a structure. Here's the structure: typedef struct { Size2f spriteSize; Vertex2f *vertices; GLubyte *vertex_indices; } tSprite; And here's...

C: problem with char*

/* * code.c * * TASK * Reverse a string by reversing pointers. Function should use return * type char* and use a char* parameter as input. */ #include <stdio.h> #include <string.h> #define STRMAX 51 char* reverse(char* sPhrase[]); int main() { char sPhrase[STRMAX]; char sReverse[STRMAX]; printf("Enter string...

Returning reference to a pointer- C++

Consider the following class. class mapping_items { public: mapping_items(){} void add(const mapping_item* item) { items_.push_back( item ); } size_t count() const{ return items_.size(); } const mapping_item& find(const std::string& pattern){ const mapping_item* item = // iterate vecto...

Returning an array of char pointers

I'm trying to return an array of char*'s to a function. I've simplified my code to a test case that clones a char array that instead of containing chars holds pointers to those chars. /* * code.c */ #include <stdio.h> char* makePointerCopy(char cIn[]); int main() { char cTest[] = {'c', 't', 's', 't'}; char* cPTest[] = makePo...

What happens if I cast a function pointer, changing the number of parameters

I'm just beginning to wrap my head around function pointers in C. To understand how casting of function pointers works, I wrote the following program. It basically creates a function pointer to a function that takes one parameter, casts it to a function pointer with three parameters, and calls the function, supplying three parameters. I ...

How to Use LLIST *mylist[N];

I get this: LLIST *mylist[N]; Where N is the number of rows of the input file. Then mylist[i] is a pointer to the ith linked list. I call a function LLIST *list_add(LLIST **p, int i){ LLIST *n; n = (LLIST *) malloc(sizeof(LLIST)); if (n == NULL) return NULL; n->next = *p; /* the previous element (*p) now ...

Weird crash with pointers / malloc() on if check of the pointer value

I have some really weird crash here, ive got it before, but i thought i fixed it by adding = NULL to the variable declarations: #include <stdlib.h> ... GLuint *TEX = NULL; int TEX_MEMSIZE = sizeof(GLuint)*1024*1024; ... void something1(){ ... TEX = (GLuint *)malloc(TEX_MEMSIZE); ... } void something2(){ ... // t...

Referring to: How to Use LLIST *mylist[N];

This does work LLIST *mylist[10] = {NULL}; But would if I wanted to do this I get errors: int x=10; LLIST *mylist[x] = {NULL}; x can be any value I'm setting it to 10 for the time being. x is going to be used as a counter. ...

Access a value from a struct via a pointer? (C++)

Here is my struct: struct Checker { short row; short col; unsigned short number; short color; }; Now, I have to also make another struct to represent a checkers board: struct Board { Checker checkers[2][13]; // Zeroth entry of 13 is not used. Checker *grid[8][8]; // each entry holds Null or an address ...

Pointer, String Problem in C

I'm using C for a class project for the first time after first learning C++, so syntax is killing me... Basically, I need to store a string given by a function into a separate variable for later use. I have an array of chars declared like this char foo[]; A function that I'm given assigns a bunch of characters into this array (or po...

How to reverse a string in place in c using pointers?

Ok, I'm learning C, and I'm trying to reverse a string in place using pointers. (I know you can use an array, this is more about learning about pointers.) I keep getting segfaults when trying to run the code below. Gcc seems not to like the *end = *begin; line. Why is that? especially since my code is nearly identical to the non-evil ...

Understanding pointers with file i/o in c++

I'm trying to get a better grasp on pointers. My class assignment was to create the function for the prototype void OpenFile(const char *fileName, ifstream &inFile). void OpenFile(const char *fileName, ifstream &inFile) { inFile.open(FILENAME, ios_base::in); if (!inFile.is_open()) { cerr << "Could not open file " << fileNam...

C++ strcpy non-constant expression as array bound

Hi I turned back to C++ after a long time in C#, PHP and other stuff and I found something strange: temp.name = new char[strlen(name) + strlen(r.name) + 1]; this compiles temp.name = (char *)malloc(sizeof(char[strlen(name) + strlen(r.name) + 1])); this doesn't (temp.name is a char *) The compiler error is error C2540: n...

allocate matrix in C

hey, i want to allocate a matrix. is this the only option: int** mat = (int**)malloc(rows * sizeof(int*)) for (int index=0;index<row;++index) { mat[index] = (int*)malloc(col * sizeof(int)); } thanks ...

Any reason to use hex notation for null pointers?

I'm currently improving the part of our COM component that logs all external calls into a file. For pointers we write something like (IInterface*)0x12345678 with the value being equal to the actual address. Currently no difference is made for null pointers - they are displayed as 0x0 which IMO is suboptimal and inelegant. Changing this ...

Performace and Sizes of Non-Clustered Indexes drops as size of clustering key increases?

Excerpt from: http://www.sqlservercentral.com/articles/Indexing/68563/ The width of the clustering key does not, however, only affect the clustered index. The clustering key, being the rows’ address, is located in every single nonclustered index. Hence a wide clustering key increases the size of all nonclustered indexes, ...

what does malloc(0) return ?

What does malloc(0) returns? Would the answer be same for realloc(malloc(0),0) ? #include<stdio.h> #include<malloc.h> int main() { printf("%p\n", malloc(0)); printf("%p\n", realloc(malloc(0), 0)); return 0; } Output from linux gcc: manav@manav-workstation:~$ gcc -Wall mal.c manav@manav-workstation:~$ ./a.out 0...