c

How to do the following with C?

Basically I want to create an int xfor every time a condition is met so something like... while(CONDITION){ if(int x = 100){ //create a new int //refrence newly created variable } } I then want to run through the loop again but testing newly created variable instead of x. Hopefully this is clear enough! ...

What is the point of the matrix stack in OpenGL?

Why would I use OpenGL's matrix stack (as opposed to wrapping my own matrix class and just using that)? I'm not really sure why it is so complicated for such a simple task. Is there's some special use case for it? If it isn't necessary, what's a good replacement for it? ...

Controlling the cursor while displaying the output of a C program in Linux.

I am writing a C program which is to be executed on the Linux terminal. The program goes into an infinite loop and prints five lines over and over again. How do I get the cursor back to the previous lines? E.g. I want to print the alphabets and replace them every 15 seconds. So, at T=0, output is sh>./a.out AA BB CC DD EE At T=15, ou...

Would this be considered a memory leak?

Consider this pointless program: /* main.c */ #include <stdlib.h> #include <unistd.h> int main(int argc, char **argv) { int i; for (i = 0; i < 1024; i++) { int pid = fork(); int status; if (pid) { wait(&status); } else { char *ptr = (char *)malloc(1024*sizeof(char...

String trimming causes memory leak?

I'm curious what the proper way to trim a string is to ensure that no memory leak occurs. I guess this may really be a question based on exactly how free() works. I've included the code for my trim() function. See below. int main() { char* testStr1 = strdup("some string"); char* testStr2 = strdup(" some string"); char* ...

C: How to read only the first word from each line?

Hi, I've done many simple procedures, but I'm only trying to read the first word into a char word[30] , from each line of a text file. I've tried, but without success. Oh, I have to reuse that char each time I read it. (To put in an ordered list each time I read it). Can anyone help me and show me a way to read this way from a file, i...

Getting the current time in milliseconds

How do I get the current time on Linux in milliseconds? This is for the purpose of testing. label1 ..... label2. ...

Error fixing ASSIGNMENT. still above my head (updated code)

First of all this is homework. I have been trying to get rid of the errors and usually screw the code up farther and end up coming back to the original code. If you could please give me some direction (in terms I can understand) I would really appreciate it. I do not think our instructor actually expects us to get it, but I still have...

Source Code Dependencies

Suppose I have a bunch of C++ files: A.cc, B.cc, C.cc, and their associated header files. A.cc makes use of classes in B.cc and so on. Now say I want to build the source files. After the preprocessing phase, can I theoretically compile (not link) all the files simultaneously? (A.cc -> A.obj, ...) I'm just wondering if there is ever a ...

Advantages and disadvantages of using strdup on a string literal

I want to be clear about all the advantages/disadvantages of the following code: { char *str1 = strdup("some string"); char *str2 = "some string"; free(str1); } str1: You can modify the contents of the string str2: You don't have to use free() Faster Any other differences? ...

Linking phase in distcc

Is there any particular reason that the linking phase when building a project with distcc is done locally rather than sent off to other computers to be done like compiling is? Reading the distcc whitepages didn't give a clear answer but I'm guessing that the time spent linking object files is not very significant compared to compilation....

Search for a word in a file and replace it in C?

How could I search for a word in a certain file, and then replace that word with a different string of text. Like for example: In this paragraph find the word iPad and replace it with iPhone. I would like a code sample in C (or C++ if it is not possible without a third party library). ...

Detached vs. Joinable POSIX threads

I've been using the pthread library for creating & joining threads in C. When should I create a thread as detached, right from the outset? Does it offer any performance advantage vs. a joinable thread? Is it legal to not do a pthread_join() on a joinable (by default) thread? Or should such a thread always use the detach() function befo...

Contents of a static library

I have a static library, say mystaticlib.a. I want to see its contents, such as the number of object files inside it. How can I do this on gcc? ...

Using Excel C API (XLLs)

I have been exploring Excel C API and am now good at getting multi-cell input, doing computations and outputing the result in a single cell. Now I want to output the result in multiple cells, for example: 1) Get a column range as input 2) Sort the numbers (in the cells) 3) Write the sorted numbers in a new column or the same column (in-...

Errors with gcc when compiling C program.

I have a custom shell program in which I have included signal.h, unistd.h, and stdio.h. I was originally working on this in RedHat Enterprise (not sure exactly what version, but not too old) and I was able to use gcc on my program and it compiled fine and ran fine. Now I moved it over to Ubuntu and gcc is giving me some errors, the fir...

C Homework, Print out the binary value of an integer

#include <stdio.h> #include <stdlib.h> // Print out the binary value of an integer void binval (int num); int get_number(); main () { int num; num = get_number(); printf("Num = %d\n",num); binval(num); } void binval (int num) { int val = 0; int test; if (!num) { printf("\n"); return; } test = num & 0x...

Return the result of sum of character arrays

Recently in an interview i was asked a question to write a function which takes two character arrays(integers) as input and returns the output character array. Function Signature: char* find_sum(char* a, char* b) How would one approach this? Example scenario: find_sum("12345","32142") = "44487" Note: The number of digits can be ...

Yoda Conditions and integer promotion

When comparing a type larger than int, with an integer constant, should I place the constant on the left or the right to ensure the correct comparison is performed? int64_t i = some_val; if (i == -1) or should it be: if (-1 == i) Are there any circumstances in which either case is not identical to comparison with -1LL (where int64_...

System Time change detection on linux

Hi, I was reading about the licensing of software and one question that came to my mind is that "how software detect the change in system time and block themselves if someone changes system time?". Since there is no other reference available(provided we don't have internet connection, otherwise we can use time servers as reference), how...