question
stringlengths
24
425
answer
stringlengths
5
405
I simply write this: ``` char* test="test"; printf("%s",test[0]); ``` it says seg fault; then I change toprintf("%s",&test[0]);the error gone But this is not what I want; the console print: "test " how to get just value "t" from that pointer?
If you want just thet, you should do: ``` printf("%c",test[0]); ``` The format%c, will print a singlechar. %swill print the entire null-terminated string.
I am using Visual studio in windows for developing C library. Can someone please provide me simple example of creating thread in windows console application and starting it. Thanks
C++ and C doesn't have support for Threads. You have to use Win32 APIs for threading support for which you can google,BING.
GCC has a statment expression feature that allows macro to be defined as: ``` #define maxint(a,b) \ ({int _a = (a), _b = (b); _a > _b ? _a : _b; }) ``` This is illegal in Visual Studio. Is there an equivalent feature in microsoft visual studio? (I know, I would better use GCC. But I am just curious). Or, do we just have to resort to inline functions?
There is no equivalent in the MS compiler to this non-standard GCC language extension.
I am using Visual studio in windows for developing C library. Can someone please provide me simple example of creating thread in windows console application and starting it. Thanks
C++ and C doesn't have support for Threads. You have to use Win32 APIs for threading support for which you can google,BING.
GCC has a statment expression feature that allows macro to be defined as: ``` #define maxint(a,b) \ ({int _a = (a), _b = (b); _a > _b ? _a : _b; }) ``` This is illegal in Visual Studio. Is there an equivalent feature in microsoft visual studio? (I know, I would better use GCC. But I am just curious). Or, do we just have to resort to inline functions?
There is no equivalent in the MS compiler to this non-standard GCC language extension.
How to set (in most elegant way) exactlynleast significant bits ofuint32_t? That is to write a functionvoid setbits(uint32_t *x, int n);. Function should handle eachnfrom0to32. Especially valuen==32should be handled.
Here's a method that doesn't require any arithmetic: ``` ~(~0u << n) ```
How to set (in most elegant way) exactlynleast significant bits ofuint32_t? That is to write a functionvoid setbits(uint32_t *x, int n);. Function should handle eachnfrom0to32. Especially valuen==32should be handled.
Here's a method that doesn't require any arithmetic: ``` ~(~0u << n) ```
When writing a basic c program. ``` #include <stdio.h> main(){ printf("program"); } ``` Is the definition ofprintfin "stdio.h" or is theprintffunction automatically linked?
Usually, instdio.hthere's only the prototype; the definition should be inside a library that your object module is automatically linked against (the various msvcrt for VC++ on Windows, libcsomething for gcc on Linux). By the way, it's<stdio.h>, not"stdio.h".
What does this do and how? ``` typedef int map_t [1<<MAX]; ``` What does that line do?
AssumingMAXis a constant known at compile-time, then this code: ``` typedef int map_t [1<<MAX]; map_t x; ``` is the same as this code: ``` int x[1 << MAX]; ```
Is it possible to execute a process whose argc = 0? I need to execute a program but it is extremely important for its argc to be equal to 0. Is there a way to do that? I tried to put 2^32 arguments in the command line so that it appears as if argc = 0 but there is a maximum limit to the number of arguments.
You can write a program that callsexecdirectly; that allows you to specify the command-line arguments (including the program name) and lack thereof.
What API is available to use Facebook from C or C++, and is there a library to do the grunt work for me? (So that you for instance can use Facebook from a desktop program written in C++.) Thankssehe.
Yes you are able to develop Facebook application using C/C++. Here's a good APIhttp://projects.seligstein.com/facebook/
If we want to write a module in C and have to compile it as C++ withg++, is it OK to develop a piece of code in C++ without any own classes, only using "global / static functions" as in C? So, simply said, to code C in C++ (with only few system header changes etc.)
Yes. In fact, it's generally a good idea because C++ enforces stronger type-checking than C.
I'm looking for a tool that can statically discover invariants in C programs. I checked outDaikonbut it discovers invariants only dynamically. Is there a tool available for what I'm looking for? Thanks!
SeeThe SLAM project: debugging system software via static analysis. It claims to infer invariants statically, for just what you asked for, the C language. The author, Tom Ball, is widely known for stellar work in program analysis.
If I have an array and want to assign pointer to id'th element of the array are these equivalent? ``` int* hello = &Array[id]; int* hello = &(Array[id]); ```
No, parentheses are not necessary, and yes these two are equivalent. Array subscripthas higher precedencethan Address operator.
What API is available to use Facebook from C or C++, and is there a library to do the grunt work for me? (So that you for instance can use Facebook from a desktop program written in C++.) Thankssehe.
Yes you are able to develop Facebook application using C/C++. Here's a good APIhttp://projects.seligstein.com/facebook/
If we want to write a module in C and have to compile it as C++ withg++, is it OK to develop a piece of code in C++ without any own classes, only using "global / static functions" as in C? So, simply said, to code C in C++ (with only few system header changes etc.)
Yes. In fact, it's generally a good idea because C++ enforces stronger type-checking than C.
I'm looking for a tool that can statically discover invariants in C programs. I checked outDaikonbut it discovers invariants only dynamically. Is there a tool available for what I'm looking for? Thanks!
SeeThe SLAM project: debugging system software via static analysis. It claims to infer invariants statically, for just what you asked for, the C language. The author, Tom Ball, is widely known for stellar work in program analysis.
If I have an array and want to assign pointer to id'th element of the array are these equivalent? ``` int* hello = &Array[id]; int* hello = &(Array[id]); ```
No, parentheses are not necessary, and yes these two are equivalent. Array subscripthas higher precedencethan Address operator.
I need to send a variable of type mode_t through a socket. How do i do it? I mean I could convert it to a string using snprintf()..but is it ok? Thanks
mode_tis an integer. Indeed, having textual protocols on sockets is practically useful (easier to debug and to evolve).
I have been googling a bit, but did not find what i was looking for. I want to start a process with arguments(in C) using the win32 API. Does anybody have some examples or pointers which i can use/see?
The win32CreateProcessfunction does what you need.
I'm new to valgrind and I was wondering if I can invoke valgrind in such a way that it causes my mallocs to fail for lack of available memory. Something like: $valgrind helloworld --heapsize=10
No, valgrind tries to not interfere with the operation of your program. You should be able to useulimit -dto restrict the amount of memory available to your program, though, independent of valgrind.
Can this cast fail and when? ``` long x=-1; long y = (long)(void*)x; assert(x==y); ``` More specifically, how to detect if the above cast is OK at compile time.
A more portable way (on the C99 standard variant) is to#include <stdint.h>and then cast pointers tointptr_t(and back). This integer type is guaranteed to be the size of a pointer.
Please highlight the difference between the following function declarations: void (*p) (void *a[], int n)void *(*p[]) (void *a, int n)
void (*p) (void *a[], int n)definesa pointer to a function that takes avoid*array and anintas parametervoid *(*p[]) (void *a, int n)definesan array of pointers to functions that return avoid*, and take avoid*and anintas parameter.
When programming in C, is it possible to set aconstwith a value of a user-input? If so, how?
Why not? ``` void some_function(int user_input) { const int const_user_input = user_input; ... return; } int main (void) { int user_input; scanf("%d", &user_input); some_function(user_input); return 0; } ```
Canepoll(on Linux) be somehow useful for regular files? I know it's primarily used with sockets but just wonder.
Not really.epollonly makes sense for file descriptors which would normally exhibit blocking behavior on read/write, like pipes and sockets. Normal file descriptors will always either return a result or end-of-file more or less immediately, soepollwouldn't do anything useful for them.
When programming in C, is it possible to set aconstwith a value of a user-input? If so, how?
Why not? ``` void some_function(int user_input) { const int const_user_input = user_input; ... return; } int main (void) { int user_input; scanf("%d", &user_input); some_function(user_input); return 0; } ```
If we want mainly anepollbased loop over file-descriptors, what else features does thelibeventoffer (not interested inhttpordnsstuff)?? I know it's quite a big project, but it looks quite simple to me to write anepollwrapper API.
epollis only available on Linux;libeventcontains some abstractions such that it'll use other similar APIs on other operating systems (for instance:kqueueon OpenBSD).
I want the user to enter two numbers with a space between them, then take the two numbers and place them in a 2-element array. For example it would look like: ``` Please enter two values: >> 1 6 ``` Wherearray[0] = 1andarray[1] = 6 How would I do this in C?
Probably usingscanf(): ``` if (scanf("%d %d\n", &i1, &i2) != 2) ...oops... ```
I read it's possible to use C/C++ code in PHP by writing an extension. Is there a simpler way, like wrapping magic or things like that (I must say, I heard the word, but don't know much about what a wrapper is).
Compiling a php extension isn't very hard. There is a little API to learn just like C programs has the main function and how to pass variables to the main function.
Imagine a function myFunctionA with the parameter double and int: ``` myFunctionA (double, int); ``` This function should return a function pointer: ``` char (*myPointer)(); ``` How do I declare this function in C?
typedefis your friend: ``` typedef char (*func_ptr_type)(); func_ptr_type myFunction( double, int ); ```
Is there a way to convert cvMat to cvMAt* in opencv? I basically have to convert a Mat object to cvMat*. So initially I convert the Mat object to a cvMat object. Now, I need to convert it into a cvMat* pointer.
This isn't an OpenCV question, it's basic C. If you're this unfamiliar with C, perhaps you should try some other solution. That said, converting fromObject somethingtoObject *somethingis as easy as passing&somethingto the function you're calling.
I have a pretty simple question. Is there a way to dynamically shift bitwise to the left OR to the right, e.g. depending on the sign of an int. ``` signed int n = 3; signed int m = -2; int number1 = 8; int number2 = 8; //number1 shift n; //number2 shift m; ``` In this case I want to shift number1 3 bits to the left and number2 2 bits to the right. Is there a way withoutif else?
For 32 bit: x = (((long long)x) << 32) >> (32 - n)
I have a pretty simple question. Is there a way to dynamically shift bitwise to the left OR to the right, e.g. depending on the sign of an int. ``` signed int n = 3; signed int m = -2; int number1 = 8; int number2 = 8; //number1 shift n; //number2 shift m; ``` In this case I want to shift number1 3 bits to the left and number2 2 bits to the right. Is there a way withoutif else?
For 32 bit: x = (((long long)x) << 32) >> (32 - n)
What is the difference between constant and restrict pointers, for example: ``` int * const ptr, int * restrict ptr ```
They do completely different things. Aconstpointer will not change. Arestrictpointer will be the only way in which the object pointed to is accessed.
Having done some C++ I have noticed that C also has structs - surely C should be considered OOP if it has them?
Because it does not have some of the basic OOPs features of:InheritancePolymorphism and so on
Having done some C++ I have noticed that C also has structs - surely C should be considered OOP if it has them?
Because it does not have some of the basic OOPs features of:InheritancePolymorphism and so on
I wanted to know more about tools that assist in writing code-generators for C. Essentially how do we achieve functionality similar to c++ templates.
Even though it's not a perfect solution and it takes some time to master it, I've used them4 macro processorin the past for generic C code generation (kinda like C++ templates). You may want to check that out.
I was going through a possible implementation method for library functionstrcpy. It is : ``` void strcpy(char *src, char *dest) { while (*dest++ = *src++) ; } ``` How can this possibly work without check of'\0'??
The result of*dest++ = *src++is the value of*srcbefore the increment ofsrc. If this value is\0, the loop terminates.
I have a bunch of file references when i run doxygen on my c code and I'd rather it not say where on my machine the file is located. how do i get rid of this in the config file? Thanks
In Doxyfile, set ``` FULL_PATH_NAMES = NO ```
I was going through a possible implementation method for library functionstrcpy. It is : ``` void strcpy(char *src, char *dest) { while (*dest++ = *src++) ; } ``` How can this possibly work without check of'\0'??
The result of*dest++ = *src++is the value of*srcbefore the increment ofsrc. If this value is\0, the loop terminates.
I have a bunch of file references when i run doxygen on my c code and I'd rather it not say where on my machine the file is located. how do i get rid of this in the config file? Thanks
In Doxyfile, set ``` FULL_PATH_NAMES = NO ```
Which character should be used forptrdiff_tinprintf? Does C standard clearly explains how to printptrdiff_tinprintf? I haven't found any one. ``` int a = 1; int b = 2; int* pa = &a; int* pb = &b; ptrdiff_t diff = b - a; printf("diff = %?", diff); // % what? ```
It's%td. Seehere.
I have a int that has values like 1235 and 12890. I want only the 1st 2 digits from this int. How can I extract it? I thought for some time and couldn't come up with any solution.
Reduce the number until you only have two digits left: ``` while (n >= 100) n /= 10; ```
Has anyone had any success with libcurl and POP3 with APOP authentication? I had success with the clear authentication but not with APOP since the library sends the USER command almost immediately after making a connection. How do I make libcurl send APOP command and stop it sending USER command?
libcurl currently doesn't support APOP. You need to dive in and make it so!
I have a int that has values like 1235 and 12890. I want only the 1st 2 digits from this int. How can I extract it? I thought for some time and couldn't come up with any solution.
Reduce the number until you only have two digits left: ``` while (n >= 100) n /= 10; ```
Has anyone had any success with libcurl and POP3 with APOP authentication? I had success with the clear authentication but not with APOP since the library sends the USER command almost immediately after making a connection. How do I make libcurl send APOP command and stop it sending USER command?
libcurl currently doesn't support APOP. You need to dive in and make it so!
How can I make sure that a pipe is closed when my C program is stopped by a SIGINT?
You could usesignal handlingfor that: ``` #include <signal.h> void sigHandler(int sig) { // Respond to the signal here. } int main(..) { signal(SIGINT, &sigHandler); .. } ```
I am doing an assignment, which require to develop a simple text editor using C/C++. But it is a GUI application, which may port to different platform, for example, windows, linux and mac. Please recommend a ui framework to serve the purpose. Thanks.
Qtis a cross-platform application and UI framework for developers using C++ or QML, a CSS & JavaScript like language. Quoted fromqt-project.org
I'm using clang's libraries to write a program that will take the parsed code and put it into a structure. is there any up to date information about clang's libraries? reference and tutorial would be nice.
Have a look at thedocand atutand atut more. Edit: Anewer tutorial. Should only be 5 days old. You might want to have a look at theclang Internals Manualand thecfe-devmailing list.
If there someone who worked with Amazon S3 API in C? I can't manage to sign my REST request proper. Can someone share his successful experience in that?
I've never tried it, but a quick Google turned upthe libs3 C library API for Amazon S3. That might make things easier, so you don't have to deal with raw HTTP requests viacurl.
I need to define an iterator structure and method in C (for a BST), so far I realise that the iterator struct must have a pointer to a current node, and possibly a parent node. Is there anything else I should have in there, or that would be good to have? Thanks
Do the BST elements have a pointer to their own parent node? If not, you'll need a stack of parent node pointers.
How do I determine if a character is a forward slash ('/ ')? I'm looking for something along the lines of: ``` if (isupper(varToTest)) {//do something} ``` but for the life of me I can't find it.
``` if (varToTest == '/') { //do something} ```
How do I determine if a character is a forward slash ('/ ')? I'm looking for something along the lines of: ``` if (isupper(varToTest)) {//do something} ``` but for the life of me I can't find it.
``` if (varToTest == '/') { //do something} ```
In a declaration such asint i, v[5], j;, how will the variables be allocated? Is the compiler allowed to change their order?
Yes, the compiler can do whatever it wants, as long as the meaning of the program stays the same. These variables might be optimized out of existence, stored only in a register, reused for other purposes, reordered for alignment requirments. (note that a compiler cannot reorder variables within a struct)
Can i go to the line of error , while compiling aC or C++project ? Usually by executingmake, and parse the error string , and go to the specific file , and the line with errors. Is there already an usable plugin ?
Yeah this is already buit into vim. After typing:maketype:cwindowto bring up the error list. You can then navigate to the errors using this window.
I got a code snippet in which there is a ``` printf("%.*s\n") ``` what does the%.*smean?
You can use an asterisk (*) to pass the width specifier/precision toprintf(), rather than hard coding it into the format string, i.e. ``` void f(const char *str, int str_len) { printf("%.*s\n", str_len, str); } ```
I have a problem assigning an array like: ``` int a[]; int b[] = {1,2,3}; &a = &b; ``` I know I could use pointers but I want to try it that way...
You can't assign arrays in C. You can copy them with thememcpy()function, declared in<string.h>: ``` int a[3]; int b[] = {1,2,3}; memcpy(&a, &b, sizeof a); ```
I want to turn off the buffering for the stdout for getting the exact result for the following code ``` while(1) { printf("."); sleep(1); } ``` The code printf bunch of '.' only when buffer gets filled.
You can use thesetvbuf function: ``` setvbuf(stdout, NULL, _IONBF, 0); ``` Here're some other links to the function. POSIXC/C++
I want to change the background texture of a Gobject clutter actor whenever it is highlighted. Is there any way i can replace the texture during runtime ?
you can use theclutter_texture_set_*family of functions, like: clutter_texture_set_from_fileclutter_texture_set_from_rgb_dataclutter_texture_set_area_from_rgb_data all documented here:http://developer.gnome.org/clutter/stable/ClutterTexture.html
I have a problem assigning an array like: ``` int a[]; int b[] = {1,2,3}; &a = &b; ``` I know I could use pointers but I want to try it that way...
You can't assign arrays in C. You can copy them with thememcpy()function, declared in<string.h>: ``` int a[3]; int b[] = {1,2,3}; memcpy(&a, &b, sizeof a); ```
I want to turn off the buffering for the stdout for getting the exact result for the following code ``` while(1) { printf("."); sleep(1); } ``` The code printf bunch of '.' only when buffer gets filled.
You can use thesetvbuf function: ``` setvbuf(stdout, NULL, _IONBF, 0); ``` Here're some other links to the function. POSIXC/C++
I want to change the background texture of a Gobject clutter actor whenever it is highlighted. Is there any way i can replace the texture during runtime ?
you can use theclutter_texture_set_*family of functions, like: clutter_texture_set_from_fileclutter_texture_set_from_rgb_dataclutter_texture_set_area_from_rgb_data all documented here:http://developer.gnome.org/clutter/stable/ClutterTexture.html
I want to turn off the buffering for the stdout for getting the exact result for the following code ``` while(1) { printf("."); sleep(1); } ``` The code printf bunch of '.' only when buffer gets filled.
You can use thesetvbuf function: ``` setvbuf(stdout, NULL, _IONBF, 0); ``` Here're some other links to the function. POSIXC/C++
I want to change the background texture of a Gobject clutter actor whenever it is highlighted. Is there any way i can replace the texture during runtime ?
you can use theclutter_texture_set_*family of functions, like: clutter_texture_set_from_fileclutter_texture_set_from_rgb_dataclutter_texture_set_area_from_rgb_data all documented here:http://developer.gnome.org/clutter/stable/ClutterTexture.html
In Visual Studio 2005, I'm trying to compile a .c file: ``` int i = 6; int a[i]; ``` It doesn't work, so which standard does my compiler follow?
Visual Studio only supports C89/90. They have no support for C99. Therefore you cannot use variable-length arrays in Visual Studio. Furthermore, Microsoft has no plans to add support for C99 in their C compiler.
When using functions such asscanfyou read bytes from a buffer where (usually) data coming from the keyboard is stored. How is this data stored? Is it stored inside a fixed size vector? Is there any way to access it directly from code?
The buffer used by the standard libraries input routines is private to the implementation of the standard library. You cannot access it other than through the published interface to the standard library.
While looking through some example C code, I came across this: ``` y -= m < 3; ``` What does this do? It it some kind of condensed for loop or something? It's impossible to google for as far as I know.
m < 3is either1or0, depending on the truth value. Soy=y-1whenm<3istrue, ory=y-0whenm>=3
Is there any way to get the number of keys from an .ini file? ie. [mysection] ``` server=192.168.1.100 port=1606 blah1=9090 temp1=abcd ``` etc I want to get number of key (from the above example it should return 4) Please, any help will be great.
Kernel32.dll exports GetPrivateProfileSection() which returns a null-separated list of name/value pairs. You can count the pairs.
Inwindows process management, if we want to pass the values of more than oneinheritable handleto a child process, how should it be done.. ? I understand that we can use STARTUPINFO to pass one handle value, but how can I pass multiple handle values to achild process..?
The command line is a convenient place to pass all sorts of information.
Is it possible in X to create a window that is not visible? I looked at the XCreateSimpleWindow API but did not find any attributes to make it hidden. Is there a way? Thanks
You may be looking for an InputOnly window. You can specify the class as InputOnly when using XCreateWindow.
Is it possible in X to create a window that is not visible? I looked at the XCreateSimpleWindow API but did not find any attributes to make it hidden. Is there a way? Thanks
You may be looking for an InputOnly window. You can specify the class as InputOnly when using XCreateWindow.
I need to make a binary plugin in C that works with both Firefox and Chromium, on Linux. Where can I find a simple example of an NPAPI plugin for Linux, written in C?
Use FireBreath the cross platform/cross browser plugin projecthttp://firebreath.org
I need to make a binary plugin in C that works with both Firefox and Chromium, on Linux. Where can I find a simple example of an NPAPI plugin for Linux, written in C?
Use FireBreath the cross platform/cross browser plugin projecthttp://firebreath.org
On a sftp server i create a "lockfile.lock" if another application is manipulating data. No in my c application i would like to check if the lockfile.lock exists and than "WAIT 5 SECONDS". How do i wait 5 seconds in c without blasting the CPU to 100%? Thanks
On windows and linux there is a system call "sleep()".Windows,Linux
When you exit the program, how do theseFILE*objects get closed and released?
They are closed by the C runtime code which is automatically linked to your program - the code that calls your main() function also calls exit() after main() returns.
``` #include<stdio.h> int main() { printf("%x",-2<<2); //left shift of a negative integer return 0; } ``` If negative integers are represented with a sign bit, then I fear the sign bit will be lost. Please explain a bit.
The behaviour is at best 'implementation defined', and possibly 'undefined behaviour'.
I know how to create ajobjectand would like to convert an existingchar **into a correspondingbyte[][]and pass it to thejobjectusing JNI. How would I go about doing it?
Thishttp://java.sun.com/developer/onlineTraining/Programming/JDCBook/jnistring.htmldescribes the about Multi-Dimensional Arrays in JNI
I have been trying to run some sample c programs that uses the cv.h library, but what happened was that the compile complains the file could not be found. So I am guessing I need to some how set the compiler's path. How do I do that?
On linux, I usepkg-configto assist me on that task: ``` g++ program.cpp -o program `pkg-config --cflags --libs opencv` ```
How do I set the size of a file in c ? Would I do this after I fopen?
Yes you would do it after fopen - you can create what is know as a sparse file ``` #include <stdio.h> int main(void) { int X = 1024 * 1024 - 1; FILE *fp = fopen("myfile", "w"); fseek(fp, X , SEEK_SET); fputc('\0', fp); fclose(fp); } ``` That should create you a file for X Byte for whatever you need, in this case it's 1MiB
I am pretty new to programming and I want to know how to detect a memory leak? If you are going to recommend a utility, please try to find one that works on Mac OS X Lion. P.S. I tried valgrind, it doesn't work on 10.7/Lion.
Valgrind is an excellent cross platform toolhttp://valgrind.org/ And best of all its Open Source
I have been trying to run some sample c programs that uses the cv.h library, but what happened was that the compile complains the file could not be found. So I am guessing I need to some how set the compiler's path. How do I do that?
On linux, I usepkg-configto assist me on that task: ``` g++ program.cpp -o program `pkg-config --cflags --libs opencv` ```
How do I set the size of a file in c ? Would I do this after I fopen?
Yes you would do it after fopen - you can create what is know as a sparse file ``` #include <stdio.h> int main(void) { int X = 1024 * 1024 - 1; FILE *fp = fopen("myfile", "w"); fseek(fp, X , SEEK_SET); fputc('\0', fp); fclose(fp); } ``` That should create you a file for X Byte for whatever you need, in this case it's 1MiB
I am pretty new to programming and I want to know how to detect a memory leak? If you are going to recommend a utility, please try to find one that works on Mac OS X Lion. P.S. I tried valgrind, it doesn't work on 10.7/Lion.
Valgrind is an excellent cross platform toolhttp://valgrind.org/ And best of all its Open Source
I'm writing a simple library for checking the internet access status of network interfaces by C on Linux. Checking the status for all network interfaces (configured or not configured) Would you please give me where i can refer ( such as documents, example code ...) or any hints?
The simplest method is probably to check for the existence of a default route through that interface. You could call out to/bin/ip routeto do this.
Can someone please shed some light about this particular for loop in matlab C ? Initial is equal to each other ! ``` for(ir = ir; ir<=temp; ir++){ } ``` Thanks in advance.
Ifiris an integer, then the loop essentially doesir = max(ir, ceil(temp))
why doesn't the following line of code work? typedef float[MAT_ROW_SIZE][MAT_COL_SIZE] mat; what should I do? (I wanna avoid defining a struct)
The identifier (the name) must come first. Try ``` typedef float mat[MAT_ROW_SIZE][MAT_COL_SIZE]; ```
I was recently asked in an interview how to set the 513th bit of achar[1024]in C, but I'm unsure how to approach the problem. I sawHow do you set, clear, and toggle a single bit?, but how do I choose the bit from such a large array?
``` int bitToSet = 513; inArray[bitToSet / 8] |= (1 << (bitToSet % 8)); ``` ...making certain assumptions about character size and desired endianness. EDIT: Okay, fine. You can replace8withCHAR_BITif you want.
I'm writing a simple library for checking the internet access status of network interfaces by C on Linux. Checking the status for all network interfaces (configured or not configured) Would you please give me where i can refer ( such as documents, example code ...) or any hints?
The simplest method is probably to check for the existence of a default route through that interface. You could call out to/bin/ip routeto do this.
Can someone please shed some light about this particular for loop in matlab C ? Initial is equal to each other ! ``` for(ir = ir; ir<=temp; ir++){ } ``` Thanks in advance.
Ifiris an integer, then the loop essentially doesir = max(ir, ceil(temp))
Is it possible to convert a hexadecimal value to its respective ASCII character, not using theString.fromCharCodemethod, in JavaScript? For example: JavaScript: ``` 0x61 // 97 String.fromCharCode(0x61) // a ``` C-like: ``` (char)0x61 // a ```
You can use the\xNNnotation: ``` var str = "\x61"; ```
What happens when I use different succesive calloc functions over the same pointer? ``` int *ptr; ptr = (int *) calloc(X, sizeof(int)); ptr = (int *) calloc(Y, sizeof(int)); ptr = (int *) calloc(Z, sizeof(int)); ``` Where X,Y,Z are three distinct values.
You will lose the connection to the previously allocated memory and you will no longer be able to free it - a memory leak
Please note that, I don't have any control over the target file. Some other process is writing that file. I just want to copy the file when other process completes the write operation. I was wondering, how i can check the write operation on a file ? Thanks !
TryF_NOTIFYargument tofcntl. Or you can tryinotifyif you know that you have a Linux kernel >= 2.6.13. I found anarticle in Linux Journalwhich describes how to use it.
I have a C string that I want to use to open that file. How do I determine if the file exists?
Use the function called access() ``` if(access(file_path, F_OK)==0) //file exists else // file does not exists ```
How is it possible to use Windows thread pools from plainC, or isC++required? Is it OK to use them with MinGW?
TheThread Pool APIis a C API, it can be used either in C or C++. Yes, it's ok to use them with MinGW—MinGW uses the same C runtime library that Microsoft's compilers use (unlike Cygwin, which uses the GNU C library).
I am writing a shell that needs to be able to restart a stopped process if the user inputs a certain command. My question is: which system call do I use to be able to give the signal of SIGCONT to the proper pid/pgid?
kill(pid, SIGCONT)
The Question 1.)Is it possible to declare a pointer variable to reference to the memory address of a constant??I've tried this beforept = &20;(**pt is a pointer variable) but it's not working , so does it mean we can't do it??By the way if it's possible how am i going to work it out??
No. You must do this: ``` const int x = 20; const int* p = &x; ```
I am writing a shell that needs to be able to restart a stopped process if the user inputs a certain command. My question is: which system call do I use to be able to give the signal of SIGCONT to the proper pid/pgid?
kill(pid, SIGCONT)
When I am debugging a program in Xcode, I can pause execution in there debugger console. Is there any way to do this using gdb from the command line? I'd like to do this so I can modify breakpoints after the program has started.
You can just press Ctrl+C and will be able to do what you ask, although it might not always work (see comments below).
I want to move a Window (HWND) in a second monitor. The thing is that I tried many things like double the resolution or putting negative values and it never was able to put the window on my second monitor. Any clue of how to do that in C/C++/c#
Screen and the Form.StartPosition should get you there. See the first answer to this post (for C#/WinForms solution):How do I ensure a form displays on the "additional" monitor in a dual monitor scenario?
When I am debugging a program in Xcode, I can pause execution in there debugger console. Is there any way to do this using gdb from the command line? I'd like to do this so I can modify breakpoints after the program has started.
You can just press Ctrl+C and will be able to do what you ask, although it might not always work (see comments below).