pointers

Addressing a single dimensional pointer array with two dimensions

I have the following homework question: Consider the following declarations and answer the question. char strarr1[10][32]; char *strarr2[10]; Are strarr1[3][4] and strarr2[3][4] both legal references? I tried compiling the code with gcc to test it. I was fairly sure that the second reference would throw an error, but it didn't. This ...

Copying a string in C

Hi, I am confused about this code: (http://www.joelonsoftware.com/articles/CollegeAdvice.html) while (*s++ = *t++); What is the order of execution? Is *s = *t first done, and then are they each incremented? Or other way around? Thanks. EDIT: And what if it was: while(*(s++) = *(t++)); and while(++*s = ++*t); ...

What are the parameters in this C qsort function call?

qsort(bt->rw[t], bt->num[t], sizeof(TRELLIS_ATOM *), (int (*)(const void *,const void *))compare_wid); bt->rw[t] is a pointer to struct pointer, bt->[num] is an int, I don't understand what that fourth parameter is, except that compare_wid is a function defined somewhere as follows: static int compare_wid( TRELLIS_ATOM* ...

Passing by reference in C

If C does not support passing a variable by reference, why does this work? #include <stdio.h> void f(int *j) { (*j)++; } int main() { int i = 20; int *p = &i; f(p); printf("i = %d\n", i); return 0; } Output $ gcc -std=c99 test.c $ a.exe i = 21 ...

Inconveniences of pointers to static variables

I often use convenience functions that return pointers to static buffers like this: char* p(int x) { static char res[512]; snprintf(res, sizeof(res)-1, "number is %d", x)); return res; } and use them all over the place as arguments to other functions: ... some_func( somearg, p(6) ); .... However, this "convenience" ha...

Calloc inside function

Looking at this question that has just been asked: http://stackoverflow.com/questions/2231317/inconveniences-of-pointers-to-static-variables would doing something like this be considered bad practice, then? char* strpart(char* string, int start, int count) { char* strtemp; int i = 0; int j = 0; int strL = strlen(string); ...

Swapping objects using pointers

I'm trying to swap objects for a homework problem that uses void pointers to swap objects. The declaration of my function has to be: void swap(void *a, void *b, size_t size); I'm not looking for the exact code how to do it so I can figure it out by myself, but I'm not sure if I understand it correctly. I found that one problem is by...

Passing in member variables of a specified class.

I want to pass in the name of a member variable. I thought I could do this by template <typename T::*> void SetVal(T::* newval) { }; This obviously doesn't work, but hopefully gets across what I'm trying to do. I want to be able to set a certain member variable of the templated class. ...

C programming pointer arrays read file

The line *array[cnt] = thing causes a seg fault and I don't know why. Any ideas to fix this? long *Load_File(char *Filename, int *Size) { FILE *fp; if((fp = fopen(Filename,"r")) == NULL) { printf("Cannot open file.\n"); exit(-1); } fscanf(fp,"%d",Size); int cnt = 0; int items = *Size; l...

Whats the significance of * at the start of a C expression?

Hi Folks, Just a quick question for folks who know a bit of c. Whats the significane fo a * at the start of an expression. as in... If (this == thisThingOverHere) ThisThing = *((WORD *) &Array[withThisPosition]); You can assume WORD is a 16-bit unsigned and Array is an 8 bit byte array. Its surprisingly difficult to try and find out ...

Why does my C++ code fail to delete all nodes in my BST?

This is supposed to traverse a BST and delete every node, including the root node. However, at the end, I get the message "root still has a left node." Why aren't all nodes deleted? void deleteTree() { deleteNode(root); if(root->right) cout << "root still has a right node" << endl; if(root->left) cout <<...

Retrieving the name of the file that is associated with a file pointer

Assume I have a file pointer FILE* myfile. Is there a way to retrieve the name of the file where myfile is reading from or writing to? ...

Little endian Vs Big endian

Lets say I have 4Byte integer and I want to cast it to 2Byte short integer. Am I right that in both (little and big endian) short integer will consist of 2 least significant bytes of this 4Byte integer? Second question: What will be the result of such code in little endian and big endian processor? int i = some_number; short s = *(...

How do I copy an array using a reference in Perl?

If I do the following, it works fine: print $ref->{element}->[0]->{data}; I would like to see how many references are in the array so that I can loop through them, but I am having a hard time doing that. Here is the code I have tried, but it doesn't work: my @array = @$ref->{element}; foreach(@array) { print $_->{data}; } I ...

Is C# Endian sensitive?

Is C# ever Endian sensitive, for example, will code such as this: int a = 1234567; short b = *(short*)&i; always assign the same value to b. If so, what value will it be? If not, what good ways are there to deal with endianness if code with pointers in? ...

Array boundaries and indexes

I had an earlier post link textwhere someone said I had my pointer initialized to the wrong element which I don't quite see why, other than they are right and it works with their correction. So here is the basic problem: If I declare an array from 0 to 30 with #define ENDPOINT 15 int record[2 * ENDPOINT + 1]; // and want a pointer to ...

Dynamic allocation (malloc) of contiguous block of memory

For an assignment, I have to allocate a contiguous block of memory for a struct, but I'm first trying to do it with a 2D array of ints first and see if I understand it correctly. We had an example in the book that creates a block of memory for the pointer array (rows), and then initializes the cols and points the pointer to them. This ...

How to add n bytes in 64 bit system

Hi, I am working in 64 bit x_86 64 bit OSX system. I am reading the file of legacy database. It is loaded to a memory block and using offsets it read to the structures. It is written in 32 bit mode. So in order to read properly in 64 bit mode, I want to add n bytes to the base address of a structure. Since pointer incement increment...

Interpretation of int (*a)[3]

When working with arrays and pointers in C, one quickly discovers that they are by no means equivalent although it might seem so at a first glance. I know about the differences in L-values and R-values. Still, recently I tried to find out the type of a pointer that I could use in conjunction with a two-dimensional array, i.e. int foo[2]...

Contiguous block of memory with malloc

I'm trying to create a contiguous block of memory in one function call that has the first part of the memory as pointer arrays to the other blocks. Basically, I'm trying to do: int **CreateInt2D(size_t rows, size_t cols) { int **p, **p1, **end; p = (int **)SafeMalloc(rows * sizeof(int *)); cols *= sizeof(int); for (end ...