c

About character pointers in C

Consider this definition: char *pmessage = "now is the time"; As I see it, pmessage will point to a contiguous area in the memory containing these characters and a '\0' at the end. So I derive from this that I can use pointer arithmetic to access an individual character in this string as long as I'm in the limits of this area. So why...

Socket programmimg in C, need sample codes and tutorials

I want to do socket programming in C. Where client and server are exchanging messages. I have the sample codes with me, but i wanted some elaborate links and tutorials for socket programming C, so that i can write effective and error free code. I will be working with WinSock library and not on Linux. Any help? ...

Replying to a client over sockets (UDP) from a different process

I have a server than is a "command handler" process. It receives messages over UDP, and delegates the work to do to a different process by communicating to that process through it's published API (whatever IPC mechanism that process employes). Our system has several cooperating processes. The result of that API call is then then sent ...

Learning C

I'm a java programmer, wishing to learn C. I want to learn about memory management, making my own data structures, so when asked in interviews, I may be better prepared. To begin with, what IDE, if any, would you recommend? Thanks ...

Why is the result of this explicit cast different from the implicit one?

Why is the result of this explicit cast different from the implicit one? #include <stdio.h> double a; double b; double c; long d; double e; int main() { a = 1.0; b = 2.0; c = .1; d = (b - a + c) / c; printf("%li\n", d); // 10 e = (b - a + c) / c; d = (long) e; printf("%li\n", d); ...

Freeing memory in C

I'm having problem with this small program: UPDATED (As per some requests, I've included everything here so to make clear what I'm doing. Sorry for it being too long): Student.h file: typedef struct Student { char *name; int age; char *major; char *toString; } *Student; extern Student newStudent(char *name, int age, char *ma...

What is the fastest way to decide if a digit appears in a string?

This simple solution popped into my mind quickly. #include <ctype.h> int digit_exists_in ( const char *s ) { while (*s) { if (isdigit(*s)) { return 1; } else { s++; } } return 0; } int main(void) { int foundDigit = digit_exists_in("abcdefg...

Error on defining an array even though its set via a Constant...

Hi, I know this is really basic, but its got me stumped... In Objective-C I'm trying to write: const int BUF_SIZE = 3; static char buffer[BUF_SIZE+1]; But I get a storage size of buffer isn't constant. How do I make Xcode realise that I'm setting it to a constant, + 1...? Or is this not possible...? Thanks...! Joel ...

Should network packet payload data be aligned on proper boundries?

If you have the following class as a network packet payload: class Payload { char field0; int field1; char field2; int field3; }; Does using a class like Payload leave the recipient of the data susceptible to alignment issues when receiving the data over a socket? I would think that the class would either need to be reo...

How to create a linux equivalent "nl" command using system calls in C?

I have an exercise to do where I need to code in C, commands equivalent to cat and nl using only system calls. The system calls given to us are open(), close(), read() and write(). I've already done the "cat" equivalent and it seems to be running fine, now I need to do the "nl" one but I'm having trouble with how am I going to write lin...

Are high resolution calls to get the system time wrong by the time the function returns?

Given a C process that runs at the highest priority that requests the current time, Is the time returned adjusted for the amount of time the code takes to return to the user process space? Is it out of date when you get it? As a measurement taking the execution time of known number of assembly instructions in a loop and asking for the ...

Cross-Platform Generic Text Processing in C/C++

Hi, What's the current best practice for handling generic text in a platform independent way? For example, on Windows there are the "A" and "W" versions of APIs. Down at the C layer we have the "_tcs" functions (like _tcscpy) which map to either "wcscpy" or "strcpy". And in the STL I've frequently used something like: typedef std::ba...

Should I learn C before learning Javascript?

Would it be better to learn C before learning any type of WEB and desktop programming? I don't know how to program, I want to learn Javascript and my friends suggested to me that I should learn C first. ...

A C daemon that does traffic shaping

Hi there... I'm working on a C daemon that monitors a configuration file (updated using a web interface) and then uses the Linux "tc" (traffic control) command to update the systems traffic shaping configuration. What's a good way to actually invoke the tc program? Should I use the exec family? Is there a library I can use that would b...

strict aliasing

in C, what exactly are the performance benefits that come with observing strict aliasing? ...

C Malloc to a Pointer Through Function Call Causes Bus Error

Due to my feeble understanding of allocating type memory to pointers, the following causes a bus error on the call to barrier_create ("hi" is never printed). typedef struct barrier barrier_t; typedef struct barrier *barrier_p; barrier_p test_barrier_p; int main(int argc, char *argv[]) { barrier_create(*test_barrier_p); } int barr...

How to detect aggressiveness of a player in a game?

I am writing a game for gameboy advance and I am implementing basic AI in the form of a binary search tree. There is 1 human player and 1 computer player. I need to figure out a way of telling how aggressive the human player is being in attacking the computer. The human will push a button to attack (and must be in a certain radius of the...

Empty structure in C

I have a structure with no member (for the moment) and I would like to know if it is possible to suppress the warning I get: warning: struct has no members Is it possible to add a member and keep the sizeof the struct zero? Any other solution? ...

What's the "hints" mean for the addrinfo name in socket programming

When doing socket programming, people always name the addrinfo struct like this: struct addrinfo hints; // get ready to connect status = getaddrinfo("www.example.net", "3490", &hints, &servinfo); I'd like to know what it stands for for better understanding. Thanks in advance. Thanks for the answers. Maybe I didn't make me clear. ...

ASM in C gives an error with -std=c99

I'm now willing to compile my project with -std=c99 and I'm facing an error I'm not understanding for the moment. This line : my_type* td = ({ register kmy_type* arg0 asm("eax"); arg0; }); gives me the following error only in C99 : warning: ISO C forbids nested functions error: syntax error before ‘asm’ error: ‘arg0’ undeclared (firs...