c

Pointers problem..

What is the difference b/w struct { float *p; }*ptr=s; *ptr->p++ and (*ptr->p)++; I understand that the former points to the next address while the latter increments the value by 1 but I cannot get how it is happening..... ...

What's the proper way to drop to a lower privilege level with setuid?

I'm writing a program in C that binds to a port < 1024. I'd like it to run at non-root privileges thereafter. I know I need to call setuid(), but with what argument? UID's vary from system to system. ...

Reading from and writing to the middle of a binary file in C/C++

If I have a large binary file (say it has 100,000,000 floats), is there a way in C (or C++) to open the file and read a specific float, without having to load the whole file into memory (i.e. how can I quickly find what the 62,821,214th float is)? A second question, is there a way to change that specific float in the file without having ...

is primitive data type in c macro?

In c, are primitive data types such as "int" "long" "char" "unsigned"... all macros? If so, where are they defined? And how are they implemented such as "int" type? ...

C Compile-Time assert with constant array

I have a very big constant array that is initialized at compile time. typedef enum { VALUE_A, VALUE_B,...,VALUE_GGF } VALUES; const int arr[VALUE_GGF+1] = { VALUE_A, VALUE_B, ... ,VALUE_GGF}; I want to verify that the array is initialized properly, something like: if (arr[VALUE_GGF] != VALUE_GGF) { printf("Error occurred. arr[VA...

error expected specifier-qualifier-list before

I tried to compile this example: #include <stdio.h> #include <stdlib.h> #include <stddef.h> main(){ size_t distance; struct x{ int a, b, c; }s_tr; distance = offsetof(s_tr, c); printf("Offset of x.c is %lu bytes\n", (unsigned long)distance); e...

Why C/C++'s #pragma_once isn't an ISO standard?

I am currently working on a big project and maintaining all those include guards makes me crazy! Writing it by hand is frustrating waste of time. Although many editors can generate include guards this doesn't help much: Editor generates guard symbol based on a filename. The problem occurs when you have headers with the same filename in...

what is (__ASSERT_VOID_CAST (0))?

From assert.h file in C: #define assert(expr) (__ASSERT_VOID_CAST (0)) I wonder what is (__ASSERT_VOID_CAST (0))? I try to find its implementation but could not find anywhere. ...

Help with C program(gcc) in Linux

Hi... I have a project for my college but unfortunately, I struggle in programming, apart from simple C programs. Anyway, the way I see it, I think the code I need should be around 20-30 lines, so if somebody can provide me some help I'll be really grateful. Here is the project: A parent process will produce 10 random integer numbers (...

Using extern in C doesn't work as expected

Hi, I have created two files: tunables.h #ifndef TUNABLES_H #define TUNABLES_H void tunables_load_conservative(); void tunables_load_aggressive(); extern int timer_x; #endif /*TUNABLES_H */ and tunables.c #include "tunables.h" int timer_x; void tunables_load_conservative(){ timer_x = 3; } void tunables_load_aggressive(){ ...

Casting const void pointer to array of const char pointers properly in C.

I have a piece of C code that looks like this: const char (*foo)[2] = bar(); Now bar() is a function that returns a (const void *). How do I properly cast this const pointer? The code produces this warning from GCC : "initialization discards qualifiers from pointer target type". Here are some of my unsuccessful attempts: const char (...

How can I concatenate two arrays in C?

How do I concatenate two arrays to get a single array containing the elements of both original arrays? ...

How do I gaussian blur an image without using any in-built gaussian functions?

I want to blur my image using the native gaussian blur formula. I read this, but I am not sure how to implement this. How do I use the formula to decide weights? [I mentioned MATLAB because I do not want to use any built in fuctions that it has] ...

close() vs. shutdown() with recv()

I have the following code: Client side: n=recv(sock,buf,1200,NULL); Server side: shutdown(sockk,SD_BOTH); The problem is, the client side recv is returning 0, meaning graceful shutdown. I need it to return -1 (I can't change the client side, I need to adapt my code to it.) What can i do to cause it returning -1? ...

Is this a good way for unconditional jump?

I have a function f( ) in a file func.c and functions f1( ), f2( ), f3() , f4( ) in another file funcs.h. (Assume that all the functions receive/return values without any loss of generality). Function f( ) calls f4( ) f4( ) calls f1( ), f2( ), f3( ) in some arbitrary order, among themselves At some point of time during the execution, f...

how to restrict an input in c

I want to prevent my program from any other types of input instead of int. How to check the type of an input without assigning it to a variable? in C ...

C: Array with elements varying in size

Is it possible to have an array of elements (structs) where the values vary in size? The issue I am faced with is that I do not know how to access an element since it requires a cast. The "array" just contains pointers to the structs. Therefore I have decided to go with void**. As not each of the elements is of the same type, the type ...

Is size_t portable?

Hello, gcc 4.4.1 C99 I am using size_t and size_t is a unsigned int. However, that depends if you are running 32 bit or 64 bit. I will be using size_t to store the size of a buffer. So I don't think this would be very portable if using across architectures. Just a question, with using size_t on either a 32 or 64 bit. What situations...

Help with Perceptron

Here is my perceptron implementation in ANSI C: #include <stdio.h> #include <stdlib.h> #include <math.h> float randomFloat() { srand(time(NULL)); float r = (float)rand() / (float)RAND_MAX; return r; } int calculateOutput(float weights[], float x, float y) { float sum = x * weights[0] + y * weights[1]; return (sum >...

how is 65 translated to 'A' character?

In ASCII, i wonder how is 65 translated to 'A' character? As far as my knowledge goes, 65 can be represented in binary but 'A' is not. So how could this conversion happen? ...