c

Passing/modifying multidimensional array to function in C

I haven't used C in 5-6 years, and feel that this is probably a really obvious answer. I thought that arrays were passed by reference automatically in C, so my code below should modify the values of the array created in main() within the change function. It changes the values of the local variable within the change function, but this is...

How to buffer stdout until stdin is closed?

Hi! I'm making myself familiar with C programming and want to write a program similar to expand. The command line tool first reads every input from stdin, processes it and writes the complete result to stdout. How can I achieve this in an elegant manner? Currently my code looks something like this. This works perfectly when processing ...

in c can I do arithmetic on void * pointers

is this valid void *p = &X; /* some thing */ p += 12; and if so what does p now point to? I have (third party) code that does this (and compiles cleanly) and my guess is that the void * was treated as a char *. My trusty K&R is silent(ish) on the topic EDIT: My little test app runs fine on gcc 4.1.1 and treats void * as char *. But ...

Getting actual keycode for ibus settings

Hi, I am able to read the ibus keys configuration to know the keys that trigger an engine ON/OFF (accessible via the property "general/hotkey" and "trigger"). that is all good but that returns lists of strings (gchar*) which combination switches ON/OFF the IME (ex: ["SHIFT+CTRL+F9", "SHIFT+UNDERSCORE",...]). How can I get the correspon...

In C, how to I get to a specified directory?

I have to do a program where I need to index the files in a specified directory. I've gotten the indexing part down, but what I'm having trouble with is how to navigate to the directory. For example, say when I start the program, it will ask "What directory would you like to index," And then the input would be "usr/Documents/CS/Assignme...

Simple make file question (C)

Hey so I a new to C but a intermediate level programmer in general. I'm looking in to make files and having issues figuring out exactly what they are for, and how to use them. So for example I am currently compiling each of my files in my project individually by typing: gcc -o newoutfilename1.out oldcfilename1.c gcc -o newoutfilename2...

help to understand macro

Hello I'm having problem to understand some piece of code in MTD driver #define ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y)) ... static struct mtd_partition my_parts[] = { { .name = "boot", .size = 0, .offset = 0, .mask_flags = MTD_WRITEABLE }, { .name = "linux", .size = 0, .offset = ...

Echo client with over 65% packet losses

Hi! I'm just wondering why my UDP server/client pair is getting 65% packet loss. It's just an echo service that sends (the client) an empty packet when it reaches EOF, and the server resets itself when he gets that empty packet. Not really homework, just frustrated why I can't get it right :( Thanks! ...

HTTP protocol: end of a message body

Hi, I built a program that parses the header and I would like to read the message body in case I receive a POST. For headers, I have been able to look for to determine when the header ends. I am having more issues for the message body. Am I supposed to look at "Content-Length" field to know when to stop reading input? In my current co...

Help creating a simple C shell

Hello everyone, I am new to C and I am trying to create a simple C shell that will allow the user to perform various functions like chdir, cd, exit, mkdir. I've posted my code below. Can anyone look through it and see what I am doing wrong? I am not sure if I am using fork and execcv correctly. Thanks! include stdio.h include stdlib....

How do I wrap a non-standard calling convention in C?

Without getting into specifics, say I need to make use of a non-standard calling convention from C code. Functions that use this convention may return multiple values on the stack. It would be simple to put each function in a wrapper that uses inline assembly to make the call, sending output via pointer parameters given to the wrapper. U...

Changing pointer address in function

What do I need to change here so that animal contains {3,4}? void funct(unsigned char *elf) { unsigned char fish[2]={3,4}; elf=fish; } int main() { unsigned char animal[2]={1,2}; funct(animal); return 0; } EDIT: I see memcpy is an option. Is there another way just manipulating pointers? ...

in C what is the proper way to pass a pointer to a string from a function to the main.

this is the rough idea of what I am trying to do: I want the pointer in main to point to the word I just in my function. my actual code is very long so please excuse this format. main() { char *word; int lim 256; *word = function(word,lim)//I am not returning the address back only the first letter } function(word,lim) { //memory allo...

Analyzing assembly code

$ gcc -O2 -S test.c -----------------------(1) .file "test.c" .globl accum .bss .align 4 .type accum, @object .size accum, 4 accum: .zero 4 .text .p2align 2,,3 .globl sum .type sum, @function sum: pushl %ebp movl %esp, %ebp movl 12(%ebp)...

why do we type-cast?

If I do: int i=3, j=10; float f; f=(i+j)/2; so f won't get value 6.5 but 6. But, f=(i+(float)j)/10;//edited would be f=6.5. What is the place where this temporary values are stored and why do we need to type-cast? ...

How to convert nsstring to cString?

I have a nsstring(filePath),which has the path to the audio file.I want open the audio file, so i want to convert the nsstring to Cstring. fopen([filePath cStringUsingEncoding:1], "r"); is the above line is correct or not,because i can also use fopen([filePath cString], "r");, In some websites it is mentioned to use UTF8stringEncodi...

Command line arguments with datafiles

If I want to pass a program data files how can I distinguish the fact they are data files, not just strings of the file names. Basically I want to file redirect, but use command line arguments so I can a sure input is correct. I have been using: ./theapp < datafile1 < datafile2 arg1 arg2 arg3 > outputfile but I am wondering is it po...

OpenGL Lighting struggles

I'm in the middle of a project teaching the basics of OpenGL. I've got most of the requirements working fine in terms of camera rotation, translation etc. However I'm struggling a lot with the lighting. This picture is a comparison of my current program (left) vs the sample solution (right). In case you can't tell, I'm getting very mo...

EnumProcesses() vs CreateToolhelp32Snapshot()

I was wondering if there are any differences - mostly performance wise - between the two Win32 API functions EnumProcesses() and CreateToolhelp32Snapshot() for enumerating all active processes and loaded modules. Or if one is better than the other to use and why. ...

Reading a file to char array then malloc size. (C)

Hey, so lets say I get a file as the first command line argument. int main(int argc, char** argv) { unsigned char* fileArray; FILE* file1 = fopen(argv[1], "r"); } Now how can I go about reading that file, char by char, into the char* fileArray? Basically how can I convert a FILE* to a char* before I know how big I need to ma...