Posts

Showing posts with the label pointers

dynamic define and allocate pointers

dynamic define and allocate pointers I want to dynamically define and allocate pointers: #include<stdio.h> #define def_var(type,name,i) type name##i #define var(name,i) name##i void main(){ int i; for (i=0;i<10;i++){ def_var(float,*ww,i)=NULL; } for (i=0;i<10;i++){ var(ww,i)=(float *)malloc(100); } } But when I compile it, lots of error come up. Can anybody help fix it? There is no point telling us there are errors if you don't include them in your question! But it's obvious the problem is that you're declaring variables in your first for loop and trying to use them in the second one. – Chris Turner Jun 29 at 15:50 for Also you're using malloc incorrectly. You need to specify the size in bytes of what you want to allocate, so to allocate 100 float you want malloc(si...

Why does C support negative array indices?

Why does C support negative array indices? From this post in SO, it is clear that C supports negative indices. Why support such a potential memory violation in a program? Shouldn't the compiler throw a Negative Index warning at least? (am using GCC) Or is this calculation done in runtime? EDIT 1: Can anybody hint at its uses? EDIT 2: for 3.) Using counters of loops in of arrays/pointers indicates Run-time Calculation of Indices. You can legitimately take the address of the Nth element of an array and then negatively index down to -N. – Hot Licks Aug 11 '13 at 14:20 @ 1) : to avoid negative indexing you can use unsigned types as an index, which also has the nice property of failing fast . – wildplasser Aug 11 '13 at 14:29 ...

How to improve code and reduce number of code?

How to improve code and reduce number of code? That code is exactly the same, so I want to refactor it in a simple way to reduce the number of lines. One thing which is different is min/max function execution. Is it available pointer to function in python to call min/max as a pointer to function like in C? def calculate_min(a, b, c, d, e, f): try: v = a[e][f] b[d] = v if np.isnan(b[d]) else min(b[d], v) #min() except KeyError as exc: logger.error("keyerror") def calculate_max(a, b, c, d, e, f): try: v = a[e][f] b[d] = v if np.isnan(b[d]) else max(b[d], v) #max() except KeyError as exc: logger.error("keyerror") Totally unrelated but you should use logger.exception() instead of logger.error() => this will add the full error message and traceback to the log, which is very useful for debugging. – bruno desthuilliers Jun 29 at 9:06 ...