c

Generating random number between [-1, 1] in C?

I have seen many questions on SO about this particular subject but none of them has any answer for me, so I thought of asking this question. I wanted to generate a random number between [-1, 1]. How I can do this? ...

What arguments does the sizeof operator take in C?

[Original title referred to 'sizeof function'.] I tried these and they all worked: char *p; printf("Size of *p is %d\n",sizeof(*p)); //result =1 printf("Size of p is %d\n",sizeof( p)); //result =4 printf("Size of p is %d\n",sizeof( //result =4 I wonder why the first printf is 1, the 2nd and 3rd is 4? So what arguments can size...

Hex to char array in C

Given a string of hex values i.e. e.g. "0011223344" so that's 0x00, 0x11 etc. How do I add these values to a char array? Equivalent to say: char array[4] = { 0x00, 0x11 ... }; ...

WinHttp: How to use a temporary certificate store?

I have a C++ application that makes a HTTPS connection to one of our servers. In my ideal world, I would like the following to occur: App Starts App makes Windows trust the server's root CA (no GUI please, just system calls) App talks to server, does its work, etc. App makes windows forget about the server's root CA done I do NOT wan...

C initialize array within structure

I want to have an variable-length array contained within a structure, but am having trouble initializing it correctly. struct Grid { int rows; int cols; int grid[]; } int main() { struct Grid testgrid = {1, 3, {4, 5, 6}}; } Everything I try gives me an 'error: non-static initialization of a flexible array member' error. ...

Can SQL Server do a type conversion at run-time

Hi, We have a utility written in C which reads columns extracted from a database using a stored procedure and outputs a csv file. Simple huh. However when reading a smallint column, it crashes out and not being the greatest C programmer on the planet, I can't nail it. As a workaround can you change the data type in a stored procedure e....

Conditionally change C statements in C source file

I have a C file in which we are moving the logging infrastructure. So if ( logging_level >= LEVEL_FINE ) printf("Value at %p is %d\n", p, i); becomes do_log2(LEVEL_FINE, "Value at %p is %d\n", _ptr(p), _num(i)); do_log2 means log with 2 arguments. So I need a C parsing and modification infrastructure to do this. Which tool can...

Removing Bit fields in a struct

Hey Folks, I have a question. I have a legacy application which used bit fields in a structure. Something like this struct sample { BYTE one: 2; BYTE two : 1; BYTE three: 5; } sampletest; So "three" can have a value of MAX 31 only. Now I have a requirement to increase the MAX value of three. I am planning to r...

Whi is nl_langinfo(CODESET) different from locale charmap?

This post originated from http://stackoverflow.com/questions/1492918/how-do-you-get-what-kind-of-encoding-your-system-uses-in-c-c I tried using nl_langinfo(CODESET) but I got ANSI_X3.4-1968 instead of UTF-8 (which is what I get when typing: locale charmap). Am I using nl_langinfo() wrong? How should I use it? ...

Memory usage of current process in C

I need to get the memory usage of the current process in C. Can someone offer a code sample of how to do this on a Linux platform? I'm aware of the cat /proc/<your pid>/status method of getting memory usage, but I have no idea how to capture that in C. BTW, it's for a PHP extension I'm modifying (granted, I'm a C newbie). If there are ...

Serial Device: Reading 8N1 works, but writing a single byte fails

Hi, In my program I read from the serial device (Linux, 8N1) without any problem. But in the case I want to write a single bit, I get nothing out on the interface. I assume that my serial output settings are wrong. But there aren't that many ways how to set c_oflag... My code: #define TTYDEVICE "/dev/ttyS0" #define BAUDRATE B9600 in...

How to get file descriptor of buffer in memory?

If I have a buffer which contains the data of a file, how can I get a file descriptor from it? This is a question derived from how to untar file in memory ...

Wrapping C in C++, just for try/catch

So, I have a big piece of legacy software, coded in C. It's for an embedded system, so if something goes wrong, like divide by zero, null pointer dereference, etc, there's not much to do except reboot. I was wondering if I could implement just main() as c++ and wrap it's contents in try/catch. That way, depending on the type of exceptio...

Kernel compilation error

Hi, I was trying to compile the Linux kernel. I am getting the following error:- "No rule to make target arch/x86/kernel/entry_32.o', needed by arch/x86/kernel/built-in.o'." Before this I configured the kernel(didn't do anything extra except the default options). configured the kernel using "make gconfig". The config was generated. I...

How to empty a char array?

Have an array of chars like char members[255]. How can I empty it completely without using a loop? char members[255]; By "empty" I mean that if it had some values stored in it then it should not. For example if I do strcat then old value should not remain members = "old value"; //empty it efficiently strcat(members,"new"); // should...

implementing the derivative in C/C++

How is derivative of a f(x) is typically calculated programmatically to ensure maximum accuracy? I am implementing the Newton-Raphson method and it requires taking of the derivative of a function. thanks ...

C sizeof char* array

I have a char* array as follows: char *tbl[] = { "1", "2", "3" }; How do I use the sizeof operator to get the number of elements of the array, here 3? The below did work, but is it correct? int n = sizeof(tbl) / sizeof(tbl[0]) ...

pointer problem in implementing Tree in C

I am implementing an avl tree for my assignment. #include <string.h> #include <stdlib.h> #include <stdio.h> #include <assert.h> struct TreeNode { char *item; struct TreeNode *left; struct TreeNode *right; signed char balance; }; typedef struct TreeNode Node; void _print_avl (Node *, int , const char *); Node * get_new_node (...

Can the C preprocessor perform integer arithmetic?

Hi, as the questions says, is the C preprocessor able to do it? E.g.: #define PI 3.1416 #define OP PI/100 #define OP2 PI%100 Is there any way OP and/or OP2 get calculated in the preprocessing phase? Thanks. ...

How to structure your c program?

I've been working/coding in C#/Java for some years, so the basics etc. don't give me to much a hard time. But I've never done anything larger than small commandline learning programs in c. Now I'm trying to make a mobile phone emulator for Linux and I have no clue how to structure my code when its not object oriented. I have 3 big books...