question
stringlengths 24
425
| answer
stringlengths 5
405
|
---|---|
I have
char* data;
which has a buffer dumped into it. I want to view the content of this char*. I do not know the size of this pointer data so How do I print it?! Thank you
| Assuming thechar*isNUL-terminated, useprintf("%s\n",data);.
If the data are notNUL-terminated, this may cause a segmentation fault.
If your data have some format other than ASCII characters, you're going to need to write some code to print it.
|
Is it a guarantee thatstateafter this code will beLX_DONE?
```
enum lx_state { LX_START, LX_MIDDLE, LX_DONE };
enum lx_state state = LX_START;
++state;
++state;
```
| Yes, the C standard says, in 6.7.2.2/3,
Each subsequent enumerator with no =
defines its enumeration constant as the value of the constant expression obtained by
adding 1 to the value of the previous enumeration constant
|
Just want to know whether the casts in the example below are redundant.
uint16_t basic_units = 4587U;
uint8_t int_val = (uint8_t) (((uint16_t ) (basic_units * 5U)) / 1000U);
| Theuint8_tcast is redundant. Theuint16_tcast may have the (un)intended consequence of truncating an intermediate value in the calculation ifbasic_units * 5Uwould overflow a normaluint16_tbefore being divided by 1000.
|
i wanted an UDisks script to list all the usb devices that are mounted on my system , which
must have ability to detect LVM devices.
Perl / C / Shell any is fine
Many thanks.
P.S: Hal is deprecated on my Gentoo and removed
| loads of info about udisks herehttp://igurublog.wordpress.com/2011/01/26/a-custom-udisks-automounter/
|
I want to combine both the for loops into single for loop. How can i do that?
I want to loop through a to z, and A to Z, like so:
```
char ch;
for (ch = 'A' ; ch <= 'Z' ; ch++ )
{
}
for (ch = 'a' ; ch <= 'z' ; ch++ )
{
}
```
but using a single loop.
| ```
for (char ch = 'A' ; ch <= 'z' ; ch == 'Z' ? ch = 'a' : ++ch )
{
}
```
Should work -- though please, please, don't inflict this on your fellow developers.
|
Is it possible to call C or C++ code from a Java Code, i can re-write the code in Java, but need to reuse the existing code..
Thanks
| Yes, it's possible using theJava Native Interface.
|
What is the portable way to check whethermallocfailed to allocate non-zero memory block?
| According to theSingle Unix Specification,mallocwill returnNULLand seterrnowhen it fails.
|
GNU Emacs is easily extended with Emacs Lisp. In some cases you may hit a performance wall even after byte compiling. In this situation it would be useful to be able to write C functions that you could call from Emacs Lisp code. Is this possible? How do you do it? Is there example code?
| Isthis (Writing Emacs Primitives)what you are looking for?
|
Is there is an equivalent in C or Objective-C to do the following (taken from Java)
```
Integer.toHexString(some_int)
```
| For 32 bit Integers:
```
char buffer[enough_space_for_the_largest_string..];
sprintf (buffer, "%08x", YourNumber);
```
|
I am looking for a good library (preferably in C/C++) that can calculate the intersections between a line segment (ray) and a trimesh.
Thanks in advance for any answers!
| CGALis probably overkill, but I suggest it just for reference. Thedocumentationis somewhat intimidating, butthis chaptercould be of interest for you.
|
I'm looking for classes like "File, Socket, String" wrapping the C (or C++) standard library functions and throwing exceptions in case of errors.
| Maybe the question wasn't clear,
btw i've found this library is exactly what i was looking for:commonc++.
Edit:Better alternative still maintained.
|
How to swap the following two hexadecimal numbers in c language? Eg:
input :0xabcdoutput:0xcdab
| You can use something along the lines of:
```
#include <stdio.h>
int main (void) {
unsigned int from = 0xabcd;
unsigned int to = ((from & 0xff) << 8) | (from >> 8);
printf ("%04x\n", to);
return 0;
}
```
This swaps the two bytes around, giving you an output ofcdab.
|
I'm looking for classes like "File, Socket, String" wrapping the C (or C++) standard library functions and throwing exceptions in case of errors.
| Maybe the question wasn't clear,
btw i've found this library is exactly what i was looking for:commonc++.
Edit:Better alternative still maintained.
|
How to swap the following two hexadecimal numbers in c language? Eg:
input :0xabcdoutput:0xcdab
| You can use something along the lines of:
```
#include <stdio.h>
int main (void) {
unsigned int from = 0xabcd;
unsigned int to = ((from & 0xff) << 8) | (from >> 8);
printf ("%04x\n", to);
return 0;
}
```
This swaps the two bytes around, giving you an output ofcdab.
|
I want to create a message server using epoll. Several devices will connect to the server and exchange simple text messages. I don't have that much experience with this and I will be very appreciated if you guys can provide me any references or tutorials related to this.
I would like to work with C. Thanks in advance.
Lee
| I would suggest you start by taking a lookhere, and keepingthisclose as a reference.
|
Is it safe when therequest_token.size()is larger thanLEN?
```
char dst[LEN];
memcpy(dst, request_token.c_str(), request_token.size());
```
| No, it's not safe; you'll cause a buffer overflow. The reason is,memcpyhas no way to know the size of your target buffer, other than the size you pass in the third argument.
|
Is detecting tabs same as detecting the spaces? i.e. for detecting a space, I would just compare the space character with its ascii number.
For a tab do I have to search for '\t' character in the file or there is some other way?
| ```
if('\t' == myChar)
```
This would work, and would be better than checking against 9 since 9 may not be a guaranteed value across all architectures.
|
I would like to add arp bindings into/proc/net/arpinC. Writing into file is not allowed, so trying to do it some other way. Any suggestions? I already know about net-tools, but havent tryed yet
| You will need to use thenetlink protocolorlibnetlink. I would recommend looking at the source for thearpcommand to see exactly how it does it.
|
I want to integrate a c language compiler in to java application to compile c sources without file creation (Like Java Compiler Api). Is there any c compiler that has entirely written in java?
| You can check this link from Google CodeC compiler written in Java
and say congratz to the developer :) -it's not me :p-
Another option is this one:JCPP
|
When using GLib 1.2'sGHashTablewith theg_hash_table_foreach()method, is it safe to remove items using theg_hash_table_remove()method?
I know that Glib 2.0 provides theg_hash_table_foreach_steal()method, but we're stuck with 1.2 for our build at work.
| Well it's not allowed in the current API, so I'd be really surprised if that functionality was there in 1.2.
|
Consider the following OpenMP for loop:
```
#pragma omp parallel for schedule(dynamic)
for (int i = 0; i < n; ++i)
{
//do something with i
}
```
Is it guaranteed that each OpenMP thread sees its i values in ascending order?
| The order in which threads run is not guaranteed;the order in which a thread processes its own chunk is guaranteed.
|
If I Make A Program In C Without Including Windows.h Header File Will It Run On Linux.
I Am Making It In Code Blocks On Windows.
| If you only use the Standard C Library you'll be fine. if you go includingio.handconio.hand other junk like that, then you won't be fine.
Obviously I am assuming you are not expecting a windows PE to run on linux (without WINE) or a linux ELF to run on windows (you have to recompile like @sehe suggests).
|
Can somebody verify whether the functiongtk_combo_box_text_append()is available from 2.24 or 3? I was playing with it last night and I got undefined reference error to the specific function.
By the way, I did the test on Slackware64 13.37 on which I checked the version of gtk+2 is 2.24.4.
| According todocumentationthere is no such function in 2.24. There is functiongtk_combo_box_text_append_textin both 2.24 and 3.
|
Is this code (simplified from a real project) correct? Will the message always print?
```
char *cp = NULL;
char **cpp = &cp;
if(*cpp == NULL) {
printf("I believe this will this always print. Does it?\n");
}
```
Thanks!
| There is nothing wrong with the code you have shown. Yourchar **pointer is pointing to a valid variable, thus it's always proper to dereference it.
P.S. Yes, it will always print.
|
I want to create pkcs7 signed envelop having S/MIME format and also want to read it. The file extension ispk7.
How to do it with the OpenSSL library?
| TheSMIME_read_PKCS7()andSMIME_write_PKCS7()functions can be used to convert an SMIME message into a PKCS7 structure and vice-versa.
You can usePKCS7_sign()/PKCS7_encrypt()to create PKCS7 structures, andPKCS7_verify()/PKCS7_decrypt()to consume them.
|
I am using gnu tool chain. How can I, at run time, find caller of a function? i.e for example function B() gets called by many functions using function pointers. Now, whenever B gets called, I want to print the callers name. I need this for debugging a certain issue.
| If you're using GNU, you can use thebacktracefunctions. There's an example of the use on that man page.
|
How can I print all global variables/local variables? Is that possible in gdb?
| Typeinfo variablesto list "All global and static variable names" (huge list.
Typeinfo localsto list "Local variables of current stack frame" (names and values), including static variables in that function.
Typeinfo argsto list "Arguments of the current stack frame" (names and values).
|
Can somebody verify whether the functiongtk_combo_box_text_append()is available from 2.24 or 3? I was playing with it last night and I got undefined reference error to the specific function.
By the way, I did the test on Slackware64 13.37 on which I checked the version of gtk+2 is 2.24.4.
| According todocumentationthere is no such function in 2.24. There is functiongtk_combo_box_text_append_textin both 2.24 and 3.
|
Is this code (simplified from a real project) correct? Will the message always print?
```
char *cp = NULL;
char **cpp = &cp;
if(*cpp == NULL) {
printf("I believe this will this always print. Does it?\n");
}
```
Thanks!
| There is nothing wrong with the code you have shown. Yourchar **pointer is pointing to a valid variable, thus it's always proper to dereference it.
P.S. Yes, it will always print.
|
I want to create pkcs7 signed envelop having S/MIME format and also want to read it. The file extension ispk7.
How to do it with the OpenSSL library?
| TheSMIME_read_PKCS7()andSMIME_write_PKCS7()functions can be used to convert an SMIME message into a PKCS7 structure and vice-versa.
You can usePKCS7_sign()/PKCS7_encrypt()to create PKCS7 structures, andPKCS7_verify()/PKCS7_decrypt()to consume them.
|
I am using gnu tool chain. How can I, at run time, find caller of a function? i.e for example function B() gets called by many functions using function pointers. Now, whenever B gets called, I want to print the callers name. I need this for debugging a certain issue.
| If you're using GNU, you can use thebacktracefunctions. There's an example of the use on that man page.
|
Is there a way or an API to install a font on the PC for every user, without logging off or restarting the PC?
| See the MSDN document,Font Installation and Deletion, for full details. In summary:
Copy the font file to the fonts directory (%windir%\fonts).CallAddFontResource()to ensure the font is immediately available.Broadcast theWM_FONTCHANGEmessage.
Thanks to @Alex K for cleaning this up and adding the fine detail.
|
I have a struct :
```
struct ABC
{
int size;
int arr[15];
};
```
I know I cannot make 'int size' as 'const int size' so how can I keep the size member from being modified accidently/intentionally.Is there a way around in C?
Thanks.
| It can be const:
```
struct ABC
{
const int size;
int arr[15];
};
int main() {
struct ABC a = {3, {1,2,3} }; // ok
a.size = 42; // error
}
```
|
This seems ambiguous to me:
```
*p1->p2
*(p1->p2) or (*p1)->p2
*p1.a
*(p1.a) or (*p1).a
```
How does the compiler interpret such expressions?
| It's all down tooperator precedence. Both->and.have higher precedence than*(in this context), so the two expressions are equivalent to:
```
*(p1->p2)
*(p1.a)
```
|
This seems ambiguous to me:
```
*p1->p2
*(p1->p2) or (*p1)->p2
*p1.a
*(p1.a) or (*p1).a
```
How does the compiler interpret such expressions?
| It's all down tooperator precedence. Both->and.have higher precedence than*(in this context), so the two expressions are equivalent to:
```
*(p1->p2)
*(p1.a)
```
|
```
#include <stdio.h>
int main()
{
int i = 10;
printf("%d\n", ++(-i)); // <-- Error Here
}
```
What is wrong with++(-i)? Please clarify.
| -igenerates a temporary and you can't apply++on a temporary(generated as a result of an rvalue expression). Pre increment++requires its operand to be an lvalue,-iisn't an lvalue so you get the error.
|
I'm writing code for a primality testing function that handles long long int's.Do I have to use special operators for such large numbers?Is there any documentation concerning large number manipulation in C?I'm using the gnu standard library.Thanks.
| No, you don't need to do anything special. You handle along long intjust the same way as you would handle aint. Just beware of overflows, as with every native integer type.
|
```
#include <stdio.h>
int main()
{
int i = 10;
printf("%d\n", ++(-i)); // <-- Error Here
}
```
What is wrong with++(-i)? Please clarify.
| -igenerates a temporary and you can't apply++on a temporary(generated as a result of an rvalue expression). Pre increment++requires its operand to be an lvalue,-iisn't an lvalue so you get the error.
|
I'm writing code for a primality testing function that handles long long int's.Do I have to use special operators for such large numbers?Is there any documentation concerning large number manipulation in C?I'm using the gnu standard library.Thanks.
| No, you don't need to do anything special. You handle along long intjust the same way as you would handle aint. Just beware of overflows, as with every native integer type.
|
I don't want to resize the entire image structure, but the images I am working with are too large to easily view with cvShowImage. Is there a way to resize their view within the window created by cvShowImage?
| No there isn't, you will have to create a new image structure and use cvResize.
|
Is there any method inCcan find a text within another text?
For example,text = "abaHello",textneedtoSearch = "Hello";.
If thetextcontains"Hello",return true, else return false.
| Usestrstr, seehttp://pubs.opengroup.org/onlinepubs/9699919799/functions/strstr.html
|
Compilers these days tend to do a significant amount of optimizations. Do they also remove unused functions from the final output?
| It depends on the compiler. Visual C++ 9 can do that - unusedstaticfunctions are removed at compilation phase (there's even aC4505 warningfor that), unused functions with external linkage can be removed at link phasedepending on linker settings.
|
```
(gdb) p (char*)0x7fffffffe9c8
$16 = 0x7fffffffe9c8 "\363\353\377\377\377\177"
```
It doesn't look like ascii nor multibyte,what's that?
| These areoctalcharacter escapes. They are usually used to insert bytes into a string that don't have a meaning as text or need to have a certain binary value.\377for instance is the hexadecimal valueffor decimal255which would be thisÿin ASCII but most likely has a very different meaning in this context.
|
```
(gdb) p (char*)0x7fffffffe9c8
$16 = 0x7fffffffe9c8 "\363\353\377\377\377\177"
```
It doesn't look like ascii nor multibyte,what's that?
| These areoctalcharacter escapes. They are usually used to insert bytes into a string that don't have a meaning as text or need to have a certain binary value.\377for instance is the hexadecimal valueffor decimal255which would be thisÿin ASCII but most likely has a very different meaning in this context.
|
consider the following two code:
```
void PrintLetter(char *src)
{
while(*src != '\0')
{
printf("%c",*src);
src++;
}
}
```
and
```
void PrintLetter(char *src)
{
int i;
for(i=0;src[i];i++)
printf("%c",src[i]);
}
```
Is there any performance difference between the two?
| None whatsoever. The compiler will perform its optimizations regardless of the form you are writing. The underlying assembly code is the same.
|
I'm learning C/C++ right now and I am reading about file operations. Suppose a program A is working with an external file (say, a text file) and another another program B is, say, trying to move the file (or worse, delete it). Is it possible to tell the OS to inform the program B that the file is in use, even though it was not created by program A?
| What you're trying to do is calledfile locking. Search for "file locking in C".
|
```
typedef struct {
float *numbers;
float val1;
float val2;
} Values;
Values val[16];
```
How can one allocate memory for numbers in the struct?
| If you want to allocate the space for one float in the first element of your struct, you would do it like this:
```
#include <stdlib.h>
/* ... */
val[0].numbers = malloc(1 * sizeof(float)); /* "1 * " for clarity... */
```
If that's what you meant.
|
are there any specifics when developing a device driver (kernel-mode) on Windows 7 32 bit or Windows 7 64 bit? Can I develop on some platform and prepare builds to run on the other one?
Thank you.
| You need theWindows Driver Kit. Yes, you should be able to cross-compile.
|
What is the cleanest way to pass data from device driver to windows service and back?
| This is normally achieved usingDevice Input and Output Control (IOCTL).
You can define your own private control code and then send information in both directions. The function is called from user mode, i.e. the service in your case.
|
C++ can use c functions byextern "C",
can c use c++ functions somehow?
| Not really. You can write a "C-compatible" function in C++, that is to say outside of any class or namespace and whose prototype does not use classes or references. If declaredextern "C"then you could call such a function from C. The function could then go on to make use of whatever C++ features were useful for it.
|
```
typedef struct {
float *numbers;
float val1;
float val2;
} Values;
Values val[16];
```
How can one allocate memory for numbers in the struct?
| If you want to allocate the space for one float in the first element of your struct, you would do it like this:
```
#include <stdlib.h>
/* ... */
val[0].numbers = malloc(1 * sizeof(float)); /* "1 * " for clarity... */
```
If that's what you meant.
|
are there any specifics when developing a device driver (kernel-mode) on Windows 7 32 bit or Windows 7 64 bit? Can I develop on some platform and prepare builds to run on the other one?
Thank you.
| You need theWindows Driver Kit. Yes, you should be able to cross-compile.
|
What is the cleanest way to pass data from device driver to windows service and back?
| This is normally achieved usingDevice Input and Output Control (IOCTL).
You can define your own private control code and then send information in both directions. The function is called from user mode, i.e. the service in your case.
|
what's the meaning ofVOID()
There are the following C code, but what's it's meaning?
```
VOID(pthread_mutex_init(&tina_mutex,MY_MUTEX_INIT_FAST));
```
| Looks like a preprocessor macro. Your editor should be able to find what it is. Or try
```
gcc -E source.c > source2.c
```
It runs the preprocessor only and replaces macros with what they really evaluate to.
|
When I want to pass a "char (*c)[10];" as a parameter,
what argument should I define in my function definition?
I mean, if I have a function:
```
void foo(*****) { ... }
```
I want to passchar (*c)[10];to it, so what do I write in place of*****?
| This should work fine:
```
void foo(char (*c)[10]);
```
|
for example,
```
int a;
```
Here there is aspacebetween 'int' and 'a'but what can be the separators other than whitespace?
| You can use a paren:
```
int main() {
int(a);
a = 42;
}
```
but please don't.
|
I used to usefflush(stdin). I read that this is not a good way to get rid of the extra characters and that it is better to use fgets like this:
```
fgets(buffer,maxsize,stdin);
```
In cases that I want to dispose of those extra chars...what kind of buffer should I use? Could I redirect in some kind of "buffer of no return"? Or do I have to use a finite size array?
Thanks in advance.
| http://c-faq.com/stdio/stdinflush2.html
|
what's the meaning ofVOID()
There are the following C code, but what's it's meaning?
```
VOID(pthread_mutex_init(&tina_mutex,MY_MUTEX_INIT_FAST));
```
| Looks like a preprocessor macro. Your editor should be able to find what it is. Or try
```
gcc -E source.c > source2.c
```
It runs the preprocessor only and replaces macros with what they really evaluate to.
|
```
i=n;
while (i>=1) {
--x=x+1;
--i=i/2;
}
```
What is the running time of this code?
A O(N^2)B O(N^3)C O(N^4)D O (LOG N)E O(2^N)
I believe it is the option D
This is for revision. Not homework
| This will never terminate as the while condition is
```
i>=i
```
However, assuming you wanted to type
```
i>=1
```
The answer will be log(n).
|
Here's all .h files I've included so far,but non have the definition ofbool:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <pthread.h>
#include <netdb.h>
#include <fcntl.h>
#include <unistd.h>
#include <event.h>
```
Which file does definebool?
| It's part ofC99and defined inPOSIX definition stdbool.h.
|
I came across a piece of codevoid *p = &&abc;. What is the significance of&&here?
I know about rvalue references but I think&&used in this context is different. What does&&indicate invoid *p = &&abc;?
| &&is gcc's extensionto get the address of the label defined in the current function.
void *p = &&abcis illegal in standard C99 and C++.
Thiscompiles with g++.
|
How can I create my own virus signature of a .exe or .lib file? I started my reading certain bytes to the file and then just storing them in another file and manually adding this to a virus scanner. Will this work? thanks
| There are many different ways to create a signature of a file, one of the simplest, and easiest, is to take a hashing function, like SHA1, and run it against the whole file.
|
Closed.This question isoff-topic. It is not currently accepting answers.Want to improve this question?Update the questionso it'son-topicfor Stack Overflow.Closed12 years ago.Improve this question
How does (exactly) utility like UNIXfilecommand guess the file type?
Are there any source-codes?
| It uses various heuristics, mainly signatures which are described in a configuration file.
```
man -s 5 magic
```
will gives the file format.
|
suppose I have a Unicode codepoint c (auint32). Is there a way to print this integer to console (as a wide character) without usinglocale.horwchar.h? Thanks.
| You can try to use
```
printf("%lc", c);
```
though you really need to make sure thatcis awint_t(which requireswchar.h) rather than auint32_t, even though the two are most likely the same type...
|
Closed.This question isoff-topic. It is not currently accepting answers.Want to improve this question?Update the questionso it'son-topicfor Stack Overflow.Closed12 years ago.Improve this question
How does (exactly) utility like UNIXfilecommand guess the file type?
Are there any source-codes?
| It uses various heuristics, mainly signatures which are described in a configuration file.
```
man -s 5 magic
```
will gives the file format.
|
suppose I have a Unicode codepoint c (auint32). Is there a way to print this integer to console (as a wide character) without usinglocale.horwchar.h? Thanks.
| You can try to use
```
printf("%lc", c);
```
though you really need to make sure thatcis awint_t(which requireswchar.h) rather than auint32_t, even though the two are most likely the same type...
|
The question is quite simple: How to read a .pgm image file into a 2D double array in C. I do not have a .pgm buffer in memory. I would like to read it from the disk into memory.
Would really appreciate if I could get a code snippet. Thank You.
| You probably won't get someone writing you all the code, but here are some useful links that might point you in the right direction:
pgm.cpgm.hPGM Format Specificationhttp://www.cplusplus.com/forum/general/2393/
|
Is there a tool around that will list all the global variables in a C program? More specifically, is there a simple commandline tool that will do this, i.e. not a heavyweight IDE, CASE, graphical toolkit system etc., but just something that can be run likefoo *.c?
| ```
ctags -R -x --sort=yes --c-kinds=v --file-scope=no file "c:\my sources" > c:\ctagop.txt
```
|
The question is quite simple: How to read a .pgm image file into a 2D double array in C. I do not have a .pgm buffer in memory. I would like to read it from the disk into memory.
Would really appreciate if I could get a code snippet. Thank You.
| You probably won't get someone writing you all the code, but here are some useful links that might point you in the right direction:
pgm.cpgm.hPGM Format Specificationhttp://www.cplusplus.com/forum/general/2393/
|
Is there a tool around that will list all the global variables in a C program? More specifically, is there a simple commandline tool that will do this, i.e. not a heavyweight IDE, CASE, graphical toolkit system etc., but just something that can be run likefoo *.c?
| ```
ctags -R -x --sort=yes --c-kinds=v --file-scope=no file "c:\my sources" > c:\ctagop.txt
```
|
When I printf a char with %c format and the char is unprintable like '\0' then there is no column of printout. Same if I use %1c. Or %1.1c. Is there a way to force printf to output a column for '\0'?
I'm doing some large printf's and I want columns to match up.
| There is nothing you can do for printf. But you can useisprintto filter the arguments of printf
```
printf("%c", (isprint(c) ? c : ' ' ));
```
|
Suppose i create a thread that ,in some point, calls a function foo().
If i call pthread_exit() from within foo, will that have as a result termination
of the thread that called foo?
thanks,
Nikos
| Of course. Otherwise what's the point of pthread_exit in the first place.http://www.kernel.org/doc/man-pages/online/pages/man3/pthread_exit.3.html"The pthread_exit() function shall terminate the calling thread"
|
In C, if my application ends unexpectedly can I call a function before that happens? I'm writing a flag into a database (processRunning = 1) that prevents other applications from starting a similar process. When the application ends it would not change that flag back.
| look into theatexitAPI of the C standard library.
|
```
#include<stdio.h>
void f(int a)
{
printf("%d", a);
}
#define f(a) {}
int main()
{
/* call f : function */
}
```
How to callf(the function)? Writingf(3)doesn't work because it is replaced by{}
| Does(f)(3);work?
The C preprocessor doesn't expand the macrofinside( ).
|
```
if ( sscanf( line, "%[^ ] %[^ ] %[^ ]", method, url, protocol ) != 3 )...
```
That format above is very strange,what's it doing?
| That line is attempting to read 3 strings that do not contain a space separated by spaces into method, url, protocol and if it fails to read 3 it will then enter the if block.
|
```
if ( fgets( line, sizeof(line), stdin ) == (char*) 0 )...
```
I don't understand what this line does,anyone knows?
| That's a rather odd way of writing a test for the return of a null pointer which indicates an error infgets().
I'd write it like this:
```
if (!fgets(line, sizeof(line), stdin))
```
|
I'm currently working on a code that uses SDL to display video and as i'm working through ssh, i'm looking for something like a text driver (just need to avoid seing the video because it's so slow).
So what I need is a simple way to avoid screen init and all that stuff that keep my framerate below 15fps :)
Does anyone know something that might help me?
| I'm not sure if that's what you want. But try:
```
export SDL_VIDEODRIVER=dummy
```
|
Is there any where to convert a openFILE*returned fromfopento aHANDLEthat is used in the Windows API functions? If so, how?
| (HANDLE)_get_osfhandle(_fileno( file ) )
Good luck on 64-bit systems if you're using Visual C++ 2008 or earlier, though, because the return type islongon those. :(
|
I'm currently working on a code that uses SDL to display video and as i'm working through ssh, i'm looking for something like a text driver (just need to avoid seing the video because it's so slow).
So what I need is a simple way to avoid screen init and all that stuff that keep my framerate below 15fps :)
Does anyone know something that might help me?
| I'm not sure if that's what you want. But try:
```
export SDL_VIDEODRIVER=dummy
```
|
Is there any where to convert a openFILE*returned fromfopento aHANDLEthat is used in the Windows API functions? If so, how?
| (HANDLE)_get_osfhandle(_fileno( file ) )
Good luck on 64-bit systems if you're using Visual C++ 2008 or earlier, though, because the return type islongon those. :(
|
I'm looking for a measure performance tool for C (I'm using MinGW windows toolchain) that gives me some results like:
Occupied memory by a variable;Cycles to run the program/a function;Spent time on a function.
Thanks
| Google Perftools is multi-platform:http://code.google.com/p/google-perftools/
GCC has profiling as well:How to use profile guided optimizations in g++?
|
I wonder there are any tools or online tools which one can construct tree just giving datas. ex ; after giving datas, I want get a picture like ;google picture
| Look intoGraphviz, and its descriptive languageDOT.
|
I want to make GUI for my application. It should work on multiple platforms. I want most of the code to be portable for all the OS (unix, windows, MAC).
GTK and GLib looks as a good solution. I want to use native APIs too
How to do this all??
| Qtmay be good for that.
|
Say I have the following code:
```
struct test* t1;
t1 = get_t(1);
```
... whereget_tis:
```
struct test* get_t(int);
```
How can I refactor the above code and put it in a function? Something like the following:
```
void r1(?* t, ?* (fn*)(int)) {
t = fn(1);
}
/* ... */
struct test* t1;
r1(t1, &get_t);
```
| usevoid *param, a pointer to anything ... commonly used in glib asgpointer
|
How can I know in a C program, on which physical processor and core my code is running?
I'm using Linux and gcc 4.4.3.
| sched_getcpu()
call returns virtual CPU number. Mapping of virtual CPU to real CPU info is in /proc/cpuinfo.
If your system supports VDSO, thensched_getcpu()is relatively fast.
CPU number can be also obtained usingCPUIDinstruction, but it is slower thansched_getcpu().
|
Is there a way to communicate with Mac OS X clipboard in language C (without Obj-C/Cocoa) ?
Thanks.
| The easiest way might be to call the command line programspbcopyandpbpaste.
|
I have to draw several thousands of pixels per frame for a falling sand animation, up to now I drawed them with Sprite.Draw (and a 1x1 Texture), but this only worked for resolutions 256x256 or lower. So is there an faster alternative to draw individual pixels?
(Shaders will not work for my problem, since the logic is applied according to the pixels state)
| You could try drawing points. I foundthisbut it is only for DirectX 9.
|
I am using the UNIXalarmsystem call to deliver a signal to the currently running process on a schedule.
The man page describes the argument to this function as being in units of seconds. I would like to have precision in milliseconds.
Is there an alternative system call on UNIX (or Linux specifically) that will allow this?
| Take a look atsetitimer() or the more modern (and more flexible)timer_setitime() APIs.
|
I want to find the length of this :
```
char *s[]={"s","a","b"};
```
it should count 4 with the /0 but the strlen or sizeof(s)/sizeof(char) gives me wrong answers..
How can i find it?
| You are making an array ofchar*and not ofchar. That's whystrlenwon't work. Use
```
sizeof(s) / sizeof(char*) //should give 3
```
If you want a single string use
```
char s[] = "sab";
```
|
I want to find the length of this :
```
char *s[]={"s","a","b"};
```
it should count 4 with the /0 but the strlen or sizeof(s)/sizeof(char) gives me wrong answers..
How can i find it?
| You are making an array ofchar*and not ofchar. That's whystrlenwon't work. Use
```
sizeof(s) / sizeof(char*) //should give 3
```
If you want a single string use
```
char s[] = "sab";
```
|
I want to find the length of this :
```
char *s[]={"s","a","b"};
```
it should count 4 with the /0 but the strlen or sizeof(s)/sizeof(char) gives me wrong answers..
How can i find it?
| You are making an array ofchar*and not ofchar. That's whystrlenwon't work. Use
```
sizeof(s) / sizeof(char*) //should give 3
```
If you want a single string use
```
char s[] = "sab";
```
|
I have a variable , Float64 min;
I want to display it only upto 1 place of decimal. Right now, I get 6 places if I use %f. How do I get only upto 1 place?
| Use:
```
printf("%.1f", min);
```
See any reference such asthisfor details.
|
I am usingsscanfin a simple little project. However, I can't see any obvious way to find where in the input string thatsscanfdecided it found all of its data and stopped processing.
Is there a way to do this?
| I think %n is what you are looking for.
Example:
```
sscanf("123", "%d%n", &i, &count);
```
This will store 3 in `count'.
|
I have an iPad app that outputs a PDF file that needs to be uploaded to a remote server. What are my options here? Is there a sFTP or FTP implementation for iOS that would help me achieve this? Can it be done using other protocol?
Thanks!
| FTP Client Wrapper in Objective-C for iPhone
ConnectionKit(might not be iOS compatible)
|
```
void TestPrint(char* format, ...)
{
va_list argList;
va_start(argList, format);
printf(format, argList);
va_end(argList);
}
int main()
{
TestPrint("Test print %s %d\n", "string", 55);
return 0;
}
```
I need to get:
```
Test print string 55
```
Actually, I get garbage output. What is wrong in this code?
| Usevprintf()instead.
|
In K&R it says that declaring a function prototype such asvoid foo();with an empty parameter list turns off function parameter type checking. Is this only forfooor for all functions?
| That only applies to the function in question, not globally.
|
I have a structure :
```
struct vertex
{
double a; double b;
}
struct polygon
{
int numofVertex;
vertex v[10];
}
```
How to send this nested structure in MPI using MPI_Send?
| Since your structure does not contain any pointers, it's easily possible.
```
MPI_Send(ptr_to_your_struct, sizeof(struct polygon), MPI_BYTES, ...);
```
|
I google it, which says it's line printer. I don't think it make any sense...
| Thelis for list and thepis for path. Check out all the othervariants, e.g. execl, execlp, execle, execv, execvp.
|
Does C supports an equivalent of the triple-slash,XML Documentation Commentsthat Visual Studio uses to provide helpful tooltips for my code in C#, or am U just spoiled by being able to use them in C#?
If it's not supported in C, are there other options?
| C does not have any equivalent of XML documentation comments or JavaDoc.
Trydoxygen.
|
hi there i can compare people birthday in format YYYY-MM-DD with string (strcmp) functions. but i need compare todays date with person's birthday to display if his/her birthday is in 7 days or not_?. i searched "time.h" library but couldn't managed it. i appreciated if you can help.
| I would usedifftimeon thetime_tvalues and compare against the number of seconds in a week...
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.