Search Results

Search found 916 results on 37 pages for 'stdio'.

Page 8/37 | < Previous Page | 4 5 6 7 8 9 10 11 12 13 14 15  | Next Page >

  • Pointers inside a structure [on hold]

    - by user3402552
    I have the next program: #include<stdio.h> #include<stdlib.h> struct a { char *ch; char *str; }; int main() { struct a s1; char ptr[100]; int m, n; printf("\n Enter a string : "); gets(ptr); m = strlen(ptr); s1.ch = (char *)malloc(strlen(ptr) * sizeof(char)); if(s1.ch) { strcpy(s1.ch, ptr); } else { printf("\n Alocation failed!\n"); } printf("\n %s\n\n", s1.ch); while(*s1.ch) { printf(" %c", *(s1.ch)); s1.ch++; } printf("\n\n"); s1.ch = s1.ch - m; printf("\n\n\n %s \n\n", s1.ch); } Is this ok this program in this way ? I mean the pointers should not be initialized ? And if it is not ok, why compile it without errors?

    Read the article

  • strlen returns incorrect value when called in gdb

    - by alesplin
    So I'm noticing some severely incorrect behavior from calls to standard library functions inside GDB. I have the following program to illustrate: #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) { char *s1 = "test"; char *s2 = calloc(strlen("test")+1,sizeof(char)); snprintf(s2,strlen("test")+1,"test"); printf("string constant: %lu\n", strlen(s1)); printf("allocated string: %lu\n", strlen(s2)); free(s2); return 0; } When run from the command-line, this program outputs just what you'd expect: string constant: 4 allocated string: 4 However, in GDB, I get the following, incorrect output from calls to strlen(): (gdb) p strlen(s1) $1 = -938856896 (gdb) p strlen(s2) $2 = -938856896 I'm pretty sure this is a problem with glibc shipped with Ubuntu (I'm using 10.10), but this is a serious problem for those of us who spend lots of time in GDB. Is anyone else experiencing this kind of error? What's the best way to fix it? Build glibc from source? (I'm already running a version of GDB built from source)

    Read the article

  • Is this buffer overflow working on Mac OSX? [migrated]

    - by cobie
    Was reading through some text and playing around with attempting to write past the size of an array in C i.e buffer overflow. The text indicates that whenever you attempt to write to say array[5] when the length of the array is 5 then you get a segmentation fault but I dont seem to be getting that When using the code below. The code actually runs. #include <stdio.h> #include <string.h> int main () { int i; int array[5] = {1, 2, 3, 4, 5}; for (i = 0; i <= 255; i++) { array[i] = 10; } int len = sizeof(array) / sizeof(int); printf("%d\n", len); printf("%d\n", array[254]); } On execution of the last statement, a 10 is printed. Am wondering whether this is a vulnerability or if there is something I am missing. I am running the code from iterm2 on a macbook pro.

    Read the article

  • How do you make a precise countdown timer using clock_gettime? [migrated]

    - by Joshun
    Could somebody please explain how to make a countdown timer using clock_gettime, under Linux. I know you can use the clock() function to get cpu time, and multiply it by CLOCKS_PER_SEC to get actual time, but I'm told the clock() function is not well suited for this. So far I have attempted this (a billion is to pause for one second) #include <stdio.h> #include <time.h> #define BILLION 1000000000 int main() { struct timespec rawtime; clock_gettime(CLOCK_MONOTONIC_RAW, &rawtime); unsigned long int current = ( rawtime.tv_sec + rawtime.tv_nsec ); unsigned long int end = (( rawtime.tv_sec + rawtime.tv_nsec ) + BILLION ); while ( current < end ) { clock_gettime(CLOCK_MONOTONIC_RAW, &rawtime); current = ( rawtime.tv_sec + rawtime.tv_nsec ); } return 0; } I know this wouldn't be very useful on its own, but once I've found out how to time correctly I can use this in my projects. I know that sleep() can be used for this purpose, but I want to code the timer myself so that I can better integrate it in my projects - such as the possibility of it returning the time left, as opposed to pausing the whole program.

    Read the article

  • Please help me debug this little C program on dynamic two-dimensional array? [migrated]

    - by azhi
    I am a newbie here. I have written a little C program, which is to create a two-dimensional matrix. Here is the code: #include <stdio.h> #include <stdlib.h> int **CreatMatrix(int m,int n){ int **Matrix; int i; Matrix=(int**)malloc(m*sizeof(int*)); for(i=0;i<m;i++){ Matrix[i]=(int*)malloc(n*sizeof(int)); } return Matrix; } int main(){ int m,n; int **A; printf("Please input the size of the Matrix: "); scanf("%d%d",&m,&n); A=CreatMatrix(m,n); printf("Please input the entries of the Matrix, which should be integers!\n"); int i,j; for(i=0;i<m;i++){ for(j=0;j<n;j++){ scanf("%d",&A[i][j]); } } printf("The Matrix that you input is:\n"); for(i=0;i<m;i++){ for(j=0;j<n;j++){ printf("%3d ",A[i][j]); } printf("\n"); } for(i=0;i<m;i++) free(A[i]); free(A); } I have run it, and it works fine. But I am not sure if it is right? Can anyone help me debug it?

    Read the article

  • increment values in printf [migrated]

    - by ravi_arya
    #include <stdio.h> int main() { int i=1; printf("%d %d %d\n",i,++i,i++); i=1; printf("%d %d %d\n",++i,i,i++); i=1; printf("%d %d %d\n",i++,++i,i); i=1; printf("%d %d %d\n",i++,i,++i); i=1; printf("%d %d %d\n",++i,i++,i); i=1; printf("%d \n",(++i)*(++i)*(++i)); i=1; printf("%d %d %d\n",++i, ++i, ++i); return 0; } Output (GCC) 3 3 1 3 3 1 2 3 3 2 3 3 3 1 3 36 4 4 4 Output(Visual Studio) 3 3 1 3 3 1 2 3 3 2 3 3 3 1 3 64 4 4 4 Can any one explain this?

    Read the article

  • a c++ program for task scheduling [closed]

    - by scheduling
    This is the code which I made but I am not able to correct the mistake in the code. Please correct the mistake in my code. #include<unistd.h> #include<stdio.h> #include<stdlib.h> #include<time.h> #include<string.h> int main() { char *timetoken; char currtime[7]; char schedtime[7]; int i; struct tm *localtimeptr; strcpy(schedtime,"15:25:00"); while(6!=9) { time_t lt; sleep(1); lt = time(NULL); localtimeptr = localtime(<); timetoken=strtok(asctime(localtimeptr)," "); for(i=1;i<5;i++) timetoken=strtok('\0'," "); if(i==3) { strcpy(currtime,timetoken); } } printf("The current time is: %s\n",currtime); printf("We are waiting for: %s\n",schedtime); if(!strcmp(currtime,schedtime)) { printf("Time to do stuff \n"); system("C:\PROJECT X"); } getch(); return 0; }

    Read the article

  • Why I am getting Presentation Error [on hold]

    - by user105697
    Below is my code. When I submit it in UVA they are giving me Presentation error. I want to know the reason. I have tried all possible ways. In my code I use 2D-array to store each word of a sentence. I also want to know the reason for giving presentation error. #include<stdio.h> #include<string.h> int main() { char a[10000],ar[100][10000]; int i,j,k,m,n,l,len[10000],c; freopen("input.txt","r",stdin); while(gets(a)) { k=0; i=0; c=0; for(j=0; ; ++k) { if(a[k]==' ' || a[k]=='\0') { ar[i][j]='\0'; len[i]=c; if(a[k]=='\0') break; i++; j=0; c=0; } else { ar[i][j]=a[k]; c++; j++; } } for(m=0; m<=i; m++) { for(n=(len[m]-1); n>=0; n--) { printf("%c",ar[m][n]); } printf(" "); } printf("\n"); } return 0; }

    Read the article

  • a c++ code for scheduling tasks [closed]

    - by scheduling
    This code has no errors but then when i execute it, there is no output and the program automatically shuts down saying the program has stopped working. #include<unistd.h> #include<stdio.h> #include<stdlib.h> #include<time.h> #include<string.h> int main() { char *timetoken; char currtime[7]; char schedtime[7]; int i; struct tm *localtimeptr; strcpy(schedtime,"15:25:00"); while(6!=9) { time_t lt; sleep(1); lt = time(NULL); localtimeptr = localtime(lt); timetoken=strtok(asctime(localtimeptr)," "); for(i=1;i<5;i++) timetoken=strtok('\0'," "); if(i==3) { strcpy(currtime,timetoken); } } printf("The current time is: %s\n",currtime); printf("We are waiting for: %s\n",schedtime); if(!strcmp(currtime,schedtime)) { printf("Time to do stuff \n"); system("C:\PROJECT X"); } getch(); return 0; }

    Read the article

  • Function not returning value at all - not a void [migrated]

    - by user105439
    I have this function that is not returning a function value. I've added some random testers to try and debug but no luck. Thanks! #include <stdio.h> #include <math.h> #include <time.h> #define N 100 float error(int a, int b); int main(){ printf("START\n"); srand(time(NULL)); int a, b, j, m; float plot[N+1]; printf("Lower bound for x: "); scanf("%d", &a); printf("Upper bound for x: "); scanf("%d", &b); printf("okay\n"); for(j = 0; j < N; j++) plot[j] = 0; printf("okay1\n"); m = error(a,b); printf("%f\n",m); return 0; } float error(int a, int b){ float product = a*b; printf("%f\n",product); return product; } so the m = error(a,b) always gives 0 no matter what! Please help. I apologise for not cleaning this up...

    Read the article

  • Cannot Open Shared Object cygmpfr-1.dll

    - by Nathan Campos
    I'm testing CeGCC, that is a gcc built to cross-compile applications to Windows CE devices. As everyone do to test compilers, I've done a Hello World program: #include <stdio.h> int main() { printf("Hello, World!"); return 0; } As I'm using Windows now(because this is my other laptop), I'm using Cygwin. But when I tried to compile I got some errors, as you can see on the terminal log: C:\Dev\WinCE\Testarm-mingw32ce-gcc test.c /opt/mingw32ce/libexec/gcc/arm-mingw32ce/4.4.0/cc1.exe: error while loading shared libraries: cygmpfr-1.dll: cannot open shared object file: No such file or directory C:\Dev\WinCE\Test What can I do?

    Read the article

  • Visual Studio Find and Replace Regular Expressions ~ find lines with quoted strings, not containing

    - by Darkoni
    Visual Studio Find and Replace Regular Expressions Find lines with quoted strings, not containing strings include or trace i am tryling to find out all lines in c++ project that contains some text as i have to use visual studio, i have to use its Find and Replace http://www.codinghorror.com/blog/2006/07/the-visual-studio-ide-and-regular-expressions.html so, for finding all lines like: print("abc"); it is enogh to write :q and it will find all quotted strings ok, but i also get lot of lines like #include "stdio.h" and trace("* step 1 *") i find out regex to get all lines containing include and trace <include|trace> so, mine question is, how to find all lines with "quotted strings" but NOT lines that contains strings include and trace. thanx

    Read the article

  • Initializing a Global Struct in C

    - by Scott
    What is the best way to accomplish the following in C? #include <stdio.h> struct A { int x; }; struct A createA(int x) { struct A a; a.x = x; return a; } struct A a = createA(42); int main(int argc, char** argv) { printf("%d\n", a.x); return 0; } When I try to compile the above code, the compiler reports the following error: "initializer element is not constant" The bad line is this one: struct A a = createA(42); Can someone explain what is wrong? I'm not very experienced in C. Thanks!

    Read the article

  • I am a beginner to C and this is the dumbest question..Confused about getchar() function

    - by happysoul
    Sorry if I am not supposed to post beginner level questions here..I am new to this site Please read the code below first I am confused about getchar() 's role in the following code.. I mean I know its helping me see the output window which will only be closed when I press enter key So getchar() is basically waiting for me to press enter and then reads a single character .. Now my question.. what is that single character this function is reading ?? I did not press any key from the keyboard for it to read Now when its not reading anything..why it does not give an error saying hey u didn't enter anything for me to read ..lol...(told u its a dumb question) #include <stdio.h> int main() { printf( "blah \n" ); getchar(); return 0; }

    Read the article

  • oracle's pro*C compiler and gnu C (__builtin_va_list, __attribute__, etc)

    - by Charles Ma
    I'm compiling a database library with pro*C which converts the .ppc library file to a .c file that gcc can use. However, I'm getting a lot of errors in pro*C like the following PCC-S-02201, Encountered the symbol "__ attribute__ " when expecting one of the following ... , Encountered the symbol "__builtin_va_list" when expecting one of the following The missing symbols are from a chain of standard includes like stdio.h and stdlib.h. How do I get around this issue? The library I'm compiling came from an old solaris system that we're now upgrading (to a new solaris 10 system) and the header files don't seem to use these symbols. e.g. the newer .h files has typedef __builtin_va_list va_list while the old .h files has typedef void* va_list There are a lot of things like this so I'm reluctant to go and fix all of them manually with a typedef

    Read the article

  • struct to bidimensional struct pointer assigment in C

    - by argos.void
    i want to get work this code and i googled and asked in efnet and freenode but i didnt find the answer. what i want i to assign a struct woot to an another bidimensional struct woot *, and i need malloc to do that. then, how can i use malloc there and how to assign the struct? thanks. #include <stdio.h> struct omg { int foo; }; struct woot { struct omg *localfoo; int foo; }; int a = sizeof(struct woot); int main(void){ struct woot *what[10][10] = (struct woot *) malloc(100*a); struct omg hahaha[100]; hahaha[1].foo = 15; what[1][6].localfoo = &hahaha[1]; }

    Read the article

  • g++ not linking header files properly

    - by cambr
    I am using cygwin libraries to run C and C++ programs on Windows. gcc runs fine, but with g++, I get a long list of errors. I think these erros are because of linking problems with C libraries. Can you suggest what I need to do to fix this? beginning error lines: In file included from testgpp.cpp:1: /cygdrive/c/cygwin/bin/../lib/gcc/i686-pc-cygwin/3.4.4/include/c++/cstdio:52:19: stdio.h: No such file or directory In file included from testgpp.cpp:1: /cygdrive/c/cygwin/bin/../lib/gcc/i686-pc-cygwin/3.4.4/include/c++/cstdio:99: error: `::FILE' has not been declared /cygdrive/c/cygwin/bin/../lib/gcc/i686-pc-cygwin/3.4.4/include/c++/cstdio:100: error: `::fpos_t' has not been declared /cygdrive/c/cygwin/bin/../lib/gcc/i686-pc-cygwin/3.4.4/include/c++/cstdio:102: error: `::clearerr' has not been declared /cygdrive/c/cygwin/bin/../lib/gcc/i686-pc-cygwin/3.4.4/include/c++/cstdio:103: error: `::fclose' has not been declared /cygdrive/c/cygwin/bin/../lib/gcc/i686-pc-cygwin/3.4.4/include/c++/cstdio:104: error: `::feof' has not been declared the whole error dump: PasteBin

    Read the article

  • Multiplying char and int together in C

    - by teehoo
    Today I found the following: #include <stdio.h> int main(){ char x = 255; int z = ((int)x)*2; printf("%d\n", z); //prints -2 return 0; } So basically I'm getting an overflow because the size limit is determined by the operands on the right side of the = sign?? Why doesn't casting it to int before multiplying work? In this case I'm using a char and int, but if I use "long" and "long long int" (c99), then I get similar behaviour. Is it generally advised against doing arithmetic with operands of different sizes?

    Read the article

  • vss intializefor backup fails with return code E_UNEXPECTED

    - by suresh
    #include "vss.h" #include "vswriter.h" #include <VsBackup.h> #include <stdio.h> #define CHECK_PRINT(result) printf("%s\n",result==S_OK?"S_OK":"error") int main(int argc, char* argv[]) { BSTR xml; LPTSTR errorText; IVssBackupComponents *VssHandle; HRESULT result = CreateVssBackupComponents(&VssHandle); CHECK_PRINT(result); result = VssHandle->InitializeForBackup(); printf("unexpected%x\n",result); system("pause"); return 0; } in the above program intializeforbackup fails with error code E_UNEXPECTED. The VSS service is running . In the event log it shows as "Volume Shadow Copy Service error: Unexpected error calling routine CoCreateInstance. hr = 0x800401f0.".. Any solutions for the InitializeForBackup to return S_OK?

    Read the article

  • getting "implicit declaration of function 'fcloseall' is invalid in C99" when compiling to gnu99

    - by Emanuel Ey
    Consider the following C code: #include <stdio.h> #include <stdlib.h> void fatal(const char* message){ /* Prints a message and terminates the program. Closes all open i/o streams before exiting. */ printf("%s\n", message); fcloseall(); exit(EXIT_FAILURE); } I'm using clang 2.8 to compile: clang -Wall -std=gnu99 -o <executable> <source.c> And get: implicit declaration of function 'fcloseall' is invalid in C99 Which is true, but i'm explicitly compiling to gnu99 [which should support fcloseall()], and not to c99. Although the code runs, I don like the have unresolved warnings when compiling. How can i solve this?

    Read the article

  • C command line password

    - by Jack Jacobsen
    So I'm trying to create a C program where you must input the password on the command line, like ./login password1 And if the password is password1, it'll say something. If not, it prints another message. This is the code I have now: #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { if (argc < 2) { printf("usage: %s <password>\n", argv[0]); } char pass = "password"; if (argc == pass) { printf("Right\n"); } else { printf("Wrong\n"); } } But it wont work.

    Read the article

  • How do functional programming languages work?

    - by eSKay
    I was just reading this excellent post, and got some better understanding of what exactly object oriented programming is, how Java implements it in one extreme manner, and how functional programming languages are a contrast. What I was thinking is this: if functional programming languages cannot save any state, how do they do some simple stuff like reading input from a user (I mean how do they "store" it), or storing any data for that matter? For example - how would this simple C thing translate to any functional programming language, for example haskell? #include<stdio.h> int main() { int no; scanf("%d",&no); return 0; }

    Read the article

  • Python: unix socket -> broken pipe

    - by Heinrich Schmetterling
    I'm trying to get Python socket working as an alternative to calling the command line socat. This socat command works fine: echo 'cmd' | sudo socat stdio <path-to-socket> but when I run this python code, I get an error: >>> import socket >>> s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) >>> s.connect(<path-to-socket>) >>> s.send('cmd') Traceback (most recent call last): File "<stdin>", line 1, in <module> socket.error: (32, 'Broken pipe') Any ideas what the issue is? Thanks.

    Read the article

  • Return a pointer to a char array in C

    - by snitko
    I've seen a lot of questions on that on StackOverflow, but reading the answers did not clear that up for me, probably because I'm a total newbie in C programming. Here's the code: #include <stdio.h> char* squeeze(char s[], char c); main() { printf("%s", squeeze("hello", 'o')); } char* squeeze(char s[], char c) { int i, j; for(i = j = 0; s[i] != '\0'; i++) if(s[i] != c) s[j++] = s[i]; s[j] = '\0'; return s; } It compiles and I get segmentation fault when I run it. I've read this faq about about returning arrays and tried the 'static' technique that is suggested there, but still could not get the program working. Could anyone point out exactly what's wrong with it and what should I be paying attention in the future?

    Read the article

  • A/UX cc compiler errors on trivial code: "declared argument argc is missing"

    - by Fzn
    On a quite ancient UNIX (Apple A/UX 3.0.1 for 680x0 processors) using the built-in c compiler (cc), this issue arrises. Here is the code I'm trying to compile: #include <stdlib.h> #include <stdio.h> int main() int argc; char **argv; { if (argc > 1) puts(argv[1]); return (EXIT_SUCCESS); } And here is the output I get: pigeonz.root # cc -c test.c "test.c", line 5: declared argument argc is missing "test.c", line 6: declared argument argv is missing Using a more modern prototype did not help, nor did the manual page, nor a quick google search. What am I doing wrong?

    Read the article

< Previous Page | 4 5 6 7 8 9 10 11 12 13 14 15  | Next Page >