When C 'deallocates' local stack variable?

Local variables exists at least (and at most) inside functions. However, what happens to block-scope variables outside block but it the same function, could I keep and use their address? Is this code valid? #include <stdio.h> int main() { ...
more »

2017-04-18 10:04 (1) Answers

placing an interval in an array of pointers

I tried emailing my Professor for help with this and he was not very helpful. given an interval of size 50 starting at x as [x,x+50] (x and x+50 are included), find where to place this interval so that it has the maximum number of elements pointed b...
more »

2017-04-17 02:04 (1) Answers

C same structure different size

My question is related to this one : c define arrays in struct with different sizes However, I do NOT want to use dynamic allocation (embedded target). Problem recap : In C, I want to have two versions of the same structure, each one with a diff...
more »

2017-04-12 07:04 (7) Answers

recursive datatypes in haskell

Consider the following definition taken from a tutorial at http://www.haskell.org : data Tree a = Leaf a | Branch (Tree a) (Tree a) fringe :: Tree a -> [a] fringe (Leaf x) = [x] fringe (Branch left ...
more »

2017-04-10 03:04 (1) Answers

Passing pointer/array to main() from a function

I’m learning functions/pointers, and having a bit of an issue. What I need to write is a C program with main() and two other functions. Requirements: read_funct() must allocate enough memory using malloc() to store the data. Function prototype f...
more »

2017-04-09 03:04 (2) Answers

Can I realloc from a position of the array?

Say I declared an array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. Later I want it to be array[8] = {2, 3, 4, 5, 6, 7, 8, 9}. Dismiss the first 2 elements. So it would start on array[2]. Reallocing array to array[2]. I tried: int *array=(int*)malloc(1...
more »

2017-04-07 15:04 (2) Answers

What is wrong with this header file?

I'm fairly new to C and am starting to learn header files. Whilst using my header I'm getting an error saying invalid type argument of '->' (have struct dirent). I don't understand what this means, I read here that the second argument to -> mus...
more »

2017-04-07 14:04 (2) Answers

Implicit conversion char[] to char* in C++

Size of char[] is number of char times sizeof(char), size of char* is sizeof(pointer) - Pointer to first element. sizeof(char[]) prints number of char times sizeof(char) in main(), where it's declared, but if I pass this array to function, it funct...
more »

2017-04-05 09:04 (2) Answers

Inserting String into String in C

Length Function: int length(const char s[]) { int length = 0; while(s[length]!='\0') { length++; } return length; } Insert Function: void insert(char s1[], const char s2[], int n) { char *beginOfString = s1; ...
more »

2017-04-04 21:04 (1) Answers

What does `(c = *str) != 0` mean?

int equiv (char, char); int nmatches(char *str, char comp) { char c; int n=0; while ((c = *str) != 0) { if (equiv(c,comp) != 0) n++; str++; } return (n); } What does "(c = *str) != 0" actually mean? Can someon...
more »

2017-03-29 12:03 (7) Answers

Interview about malloc in function

I faced a question in an interview of a company, and I cannot figure out the reason about the answer. void newArray(int* local, int size) { local = (int*) malloc( size * sizeof(int) ); } int main() { int* ptr; newArray(ptr, 10); } ...
more »

2017-03-22 17:03 (5) Answers

Double-asterisks and `malloc` in C

I've been studying pointers for a while and I can't seem to wrap my head around it fully. There seems to be a gap that isn't explained when you jump from tutorials explaining pointers to actual functions and code that assume you know about them. The ...
more »

2017-03-22 04:03 (5) Answers

Pointers with If statements in C

I am writing a text-based game in C. At the start of the program, it prompts you if you want to start or end the game. However, when I type end or start, it brings up a "Segmentation Fault" error. Here is my code: #include <stdio.h> #include &...
more »

2017-03-21 03:03 (2) Answers

Qsort and Comparators weird behaviour. C

So, I am using qsort in my C program from C library. It works as expected so I decided to play around with comparators. Comparator 1 (I use this): int compare (const void * a, const void * b) { if (*(double*)a > *(double*)b) return 1; else ...
more »

2017-03-18 23:03 (1) Answers

Passing pointer to an array into a function (C++)

I'm trying to pass an array into my function calls for build_max_heap and max_heapify so I can modify the array after each call, but I receive an error saying "candidate function not viable: no known conversion from 'int [9]' to 'int *&'for 1st a...
more »

2017-03-18 03:03 (1) Answers

"Dynamic Inheritance" in C

I wrote the following code and it works, but I would like to know if a can be sure that it works all the time on all x86 machines. #include <stdio.h> #include <stdlib.h> typedef struct Base { int a; float b; } Base; typedef struct ...
more »

2017-03-15 23:03 (2) Answers

code crashes when accessing a predefined array

I'm having trouble understanding why my code crashes when accessing the array. When converting the function param to int[3][3] instead of int** I get no problems, but I can't understand why, since the int** I'm given as a argument is a valid pointer ...
more »

2017-03-15 08:03 (2) Answers

Using a pointer to a char array in C++

I'm relativly new to Coding and have some Problems understanding how pointers work in combination with arrays (whom on their own I i understand). I understood that it's possible to create an array of pointers like: #include <iostream> using n...
more »

2017-03-04 10:03 (4) Answers

Why can't we generalize the pointer declaration?

The size of pointer is same irrespective of datatype it is pointing to.Then why do we need to declare the datatype it points to? For example, int *p; //p will point to an integer data type. char *p; //p will point to a character Then,why can't ...
more »

2017-02-27 18:02 (6) Answers