Search Results

Search found 1848 results on 74 pages for 'printf'.

Page 59/74 | < Previous Page | 55 56 57 58 59 60 61 62 63 64 65 66  | Next Page >

  • Same memory space being allocated again & again

    - by shadyabhi
    In each loop iteration, variable j is declared again and again. Then why is its address remaining same? Shouldn't it be given some random address each time? Is this compiler dependent? #include<stdio.h> #include<malloc.h> int main() { int i=3; while (i--) { int j; printf("%p\n", &j); } return 0; } Testrun:- shadyabhi@shadyabhi-desktop:~/c$ gcc test.c shadyabhi@shadyabhi-desktop:~/c$ ./a.out 0x7fffc0b8e138 0x7fffc0b8e138 0x7fffc0b8e138 shadyabhi@shadyabhi-desktop:~/c$

    Read the article

  • Using Classes / OOP in PHP

    - by Alex Demchak
    I'm pretty proficient with PHP, outside of OOP - which I am just now starting to jump in to. I've been watching videos and reading tutorials, but they are all still pretty confusing... If I have FILE 1 (class.time.php) class Time { function GetTime(){ $time = date('H:i:s'); printf($time); } } and then in a nother php page I've got FILE 2 (page.php) I can do include('class.time.php'); and then anywhere in this page I can then do $time = new Time; //Calling the class and setting the class to a variable $time->GetTime(); //This is BASICALLY saying (run the 'GetTime' function in the 'Time Class' My main question is, is the comment above (This is BASICALLY saying.....) correct? or is there a better way to think of it?

    Read the article

  • malloc unable to assign memory + doesnt warn

    - by sraddhaj
    char *str=NULL; strsave(s,str,n+1); printf("%s",str-n); when I gdb debug this code I find that the str value is 0x0 which is null and also that my code is not catching this failed memory allocation , it doesnt execute str==NULL perror code ...Any idea void strsave(char *s,char *str,int n) { str=(char *)malloc(sizeof(char)* n); if(str==NULL) perror("failed to allocate memory"); while(*s) { *str++=*s++; } *str='\0'; }

    Read the article

  • argument promotions in C function calls

    - by HaoCheng
    I learned from ----As to when default promotions kick in: default argument promotions are used exactly when the expected type of the argument is unknown, which is to say when there's no prototype or when the argument is variadic. But an example confusing me is: void func(char a, char b) { printf("a=%p,b=%p\n",&a,&b); } int main(void) { char a=0x11,b=0x22; func(a,b); return 0; } It is cleard in the above example: when calling func in main, there is no need to promote the arguments a and b, but the output shows &a = &b +4 not &a = &b+1. If no promotion occured, why 4 bytes between two CHAR argument?

    Read the article

  • Why does this C program compile?

    - by AdmiralJonB
    I've just come across someone's C code that I'm confused as to why it is compiling. There are two points I don't understand. First, the function prototype has no parameters compared to the actual function definition. Secondly, the parameter in the function definition doesn't have an type. #include <stdio.h> int func(); int func(param) { return param; } int main() { int bla = func(10); printf("%d",bla); } Could someone please explain to me why this works? I've tested it in a couple of compilers and it works fine.

    Read the article

  • Reading variable with double float precision from a text file with gnuplot

    - by user3636322
    I have a text file, containing data in 3 columns like below: 0.0100000000 | 0.0058077299 | -0.0000000288 0.0110000000 | 0.0075128707 | -0.0000000373 0.0120000000 | 0.0093579693 | -0.0000000465 I want to get the variables from this file in gnuplot and use them to draw graphs: What I exactly do is like below (e.g: to pick the variable from row 2 column 3): ii= 2 a_0 = system("awk '{ if (NR == " . ii . ") printf \"%f\", $3}' " .datafile) a_0 = a_0+0. but what is written as a_0 is zero! How can I increase the precision to get the exact value?

    Read the article

  • Is it a header file or library? in a makefile

    - by gccinac
    I already know the differences between a header file and a library. However, when I'm writing my makefile, I have some difficulties on deciding if I should put something as a dependency of the file or just at the linking rule. For example: I have 2 simple files: main.c: #include <stdio.h> main(){ printf("this is the sine or 90"); sinus(90); } and func.c: #include <math.h> sinus(int num){ return sin(num); } and my makefile is: main: main.o func.o gcc main.o func.o -lm -o main func.o: func.c main.o: main.c Well, my question is why this makefile works and this one doesn't: main: main.o func.o gcc main.o func.o -lm -o main func.o: func.c math.h main.o: main.c

    Read the article

  • Extract data from uint8 to double

    - by HADJ AMOR HASSEN
    I have a C function receiving a uint8 pointer with another parameter which is its size (number of bytes). I want to extract double data from this buffer. Here is my code: Write(uint8* data, uint8 size) /* data and size are given by a callback to my function)*/ { double d; for (i = 0; i < size; i++) { d = ((double*)&data)[i]; printf(" d = %d\n"); } } The problem is that I am not receiving what I am sending within an external hardware. I guess that my cast is wrong. I tried other methods but without any good result. I am still not able to get what I send.

    Read the article

  • Enter custom file name to be read ?

    - by ZaZu
    Hello, I want to allow users to type the name of any .txt file to be read/written. This is my code : printf("Enter .txt file name\n"); scanf("%s",&fname); FILE *inputf; inputf=fopen(&fname,"w"); Problem is this method does not work (having &fname) as a parameter. I can imagine its because C needs "filename.txt" for it work ... even if I enter for example : "custom.txt", the program returns an error of "Storage block not big enough for this operation" What is the correct method to accomplish this ? Im using C and im pretty much using basic commands .. (not too advanced) Thanks alot !!!

    Read the article

  • define macro in C wont work

    - by Spidfire
    Im trying to make a macro in C that can tell if a char is a hex number ( 0-9 a-z A-Z) #define _hex(x) (((x) >= "0" && (x) <= "9" )||( (x) >= "a" && (x) <= "z") || ((x) >= "A" && (x) <= "Z") ? "true" : "false") this what ive come up with but it wont work with a loop like this char a; for(a = "a" ; a < "z";a++){ printf("%s => %s",a, _hex(a)); } but it gives a error test.c:8: warning: assignment makes integer from pointer without a cast test.c:8: warning: comparison between pointer and integer test.c:9: warning: comparison between pointer and integer test.c:9: warning: comparison between pointer and integer test.c:9: warning: comparison between pointer and integer test.c:9: warning: comparison between pointer and integer test.c:9: warning: comparison between pointer and integer test.c:9: warning: comparison between pointer and integer

    Read the article

  • Reading from a file into an array in c

    - by NaNa21
    My file contains a series of numbers (integer, float, integer, float ....), each written on a separate line. The numbers of columns are different from one line to another i.e. 1 2.45 3 1.75 5 3.45 7 2.55 9 3.25 6 1.75 4 3.55 6 2.55 9 2.45 The program should read the contents of the entire file and place the data into an array of type float with an entry for each line. Here is my basic solution, but this is only suitable if I have fixed no of columns. float Read(FILE *pFile) { char line[50]; char letter[5]; fi = fopen("file.txt", "r"); while (fgets(line,200,fi)!=NULL) { sscanf(line,"%f %f %f",&a[i], &a2[i],&a3[i]); printf("%2.0f %2.5f %2.0f\n",a[i],a2[i],a3[i]); } fclose(fi); return a[i]; } Please HELP.

    Read the article

  • C++: use array of strings wrapped in namespace?

    - by John D.
    I got the following code, wishing to wrap a group of strings nicely in a namespace: namespace msgs { const int arr_sz = 3; const char *msg[arr_sz] = {"blank", "blank", "blank" }; msg[0] = "Welcome, lets start by getting a little info from you!\n"; msg[1] = "Alright, bla bla bla.."; msg[2] = "etc."; } The code inside works nicely inside a function, but I don't know how to return an array from it. The namespace idea LOOKS fine, but it returns on the last three lines: error: expected constructor, destructor, or type conversion before ‘=’ token Why can't I define the array inside a namespace, do I need to do something first? It's nice because I can call it like printf(msgs::msg[1]) etc. I want to do this I just can't wrap my head around what's wrong :(

    Read the article

  • Increase a recive buffer in UDP socket

    - by unresolved_external
    I'wm writing an app, which transmits video and obviously uses UDP protocol fot this purpose. So I am wondering how can I increase a size of send/recieve buffer, cause currently the maximal size of data, which I can send is 65000 bytes. I already tried to do it in following way: int option = 262144; if(setsockopt(m_SocketHandle,SOL_SOCKET,SO_RCVBUF ,(char*)&option,sizeof(option)) < 0) { printf("setsockopt failed\n"); } But it did not work. So how can I do it?

    Read the article

  • pointers for getting elements of an array in C

    - by Manolo
    I am a newbie in C and I would like to get the elements of an array with a function, I have tried different options, but I still do not get the elements. My function is: void getelements(int *a, int cl) { int *p; for (p=&a[0];p<&a[cl];p++) { printf("%d\n",*p); } } I know that the solution should work like that, but it only prints the first element and then memory positions. I am calling my function with: int v={10,12,20,34,45}; getelements(&v,5); Any help? I need to use arithmetic of pointers. Thanks

    Read the article

  • Referencing an array to a pointer

    - by james
    I want to refer a pointer to an array by another pointer. Example: void exp() { double var[2]; exp1(&var[0]); printf("\n varvalue is %lf\n",var[0]); } void exp1(double *var) { //updating the value *var[0]=4.0; exp2(&var[0]); } void exp2(double *var) { *var[0]=7.0; } This should update the value as 7.0(the last update).I am getting an array like invalid argument type of unary(*) . How can i correct this?where i am going wrong here?

    Read the article

  • Bash file descriptor leak

    - by Charles Duffy
    I get a file descriptor leak when running the following code: function get_fd_count() { local fds cd /proc/$$/fd; fds=( * ) # avoid a StackOverflow source colorizer bug echo "${#fds[@]}" } function fd_leak_func() { echo ">> Current FDs: $(get_fd_count)" read retval new_state < <(set +e; new_state=$(echo foo); retval=$?; printf "%d %s\n" $retval $new_state) } function parent_func() { while fd_leak_func; do :; done } parent_func Tested on both 3.2.25 and 4.0.28. Taking the while loop out of parent_func and running it at top level makes the problem go away. What's going on here? More to the point, are workarounds available?

    Read the article

  • Can func get the lineno who call itself? (C/C++)

    - by kingkai
    Hi, I've a problem , as the following code discribe itself. 1 #include<stdlib.h> 2 #include<stdio.h> 3 void log() 4 { 5 printf("Log [Line:%d]\n",__LINE__); 6 } 7 int main() 8 { 9 log(); 10 log(); 11 } The expected result is Log [Line:9] Log [Line:10] But, the fact is Log [Line:5] Log [Line:5] No surprising, LINE has been substituted at the pre-process stage as 5. My Question is, how to design the log function to get the expected result? Thanks!

    Read the article

  • cygwin c sem_init

    - by RileyVanZeeland
    if((sem_init(sem, 1, 1)) == 1) perror("error initiating sem"); If I include this line of code my program simply starts and exits. I just started learning how to use semaphores. I'm using cygwin and when this line is commented out the printf's ABOVE this print to console but when include this, nothing happens. I did the following to get cygserver going- CYGWIN=server ran /bin/cygserver-config ran /usr/sbin/cygserver for the config it said the cygserver is already running And for the sygserver it saids- initailaizing complete failed to created named pipe: is the daemon already running? fatal error on IPC transport: closing down Any ideas?

    Read the article

  • Getting MATLAB variable (string) from C

    - by Hamming
    Hi! I'm writing a small C application that launchs a Matlab script (.m file). I need to exchange some variables and I don't know how to get an array of chars that exists in Matlab. I'm doing something like this: enter code here result = engGetVariable(ep,"X"); if (!result) { printf ("Error..."); exit -1; } int n = mxGetN(result); char *varx = NULL; memcpy(varx, mxGetData(result),n*sizeof(char)); It doesn't work. Does someone know how to get a Matlab string in C? I've read Matlab documentation about engGetVariable() and the provided example but any of this things clarify me.

    Read the article

  • Memory allocated with malloc does not persist outside function scope?

    - by PM
    Hi, I'm a bit new to C's malloc function, but from what I know it should store the value in the heap, so you can reference it with a pointer from outside the original scope. I created a test program that is supposed to do this but I keep getting the value 0, after running the program. What am I doing wrong? int f1(int * b) { b = malloc(sizeof(int)); *b = 5; } int main() { int * a; f1(a); printf("%d\n", a); return 0; }

    Read the article

  • Problem with Initializing Consts

    - by UdiM
    This code, when compiled in xlC 8.0 (on AIX 6.1), produces the wrong result. It should print 12345, but instead prints 804399880. Removing the const in front of result makes the code work correctly. Where is the bug? #include <stdio.h> #include <stdlib.h> #include <string> long int foo(std::string input) { return strtol(input.c_str(), NULL, 0); } void bar() { const long int result = foo("12345"); printf("%u\n", result); } int main() { bar(); return 0; } Compilation command: /usr/vacpp/bin/xlC example.cpp -g

    Read the article

  • How do I make child thread exit when main thread exit?

    - by httpinterpret
    void forloop2() { int i = 0; while(TRUE) { printf("forloop2\n"); } } int main() { GThread *Thread1; GtkWidget *window; g_thread_init(NULL); gdk_threads_init(); gdk_threads_enter (); Thread1 = g_thread_create((GThreadFunc)forloop2, NULL, TRUE, NULL); gtk_init(NULL, NULL); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_widget_show_all (window); gtk_main(); g_thread_join(Thread1); gdk_threads_leave (); } When I close the window, how to make Thread1 also exit?

    Read the article

  • Difficulty understanding behavior of free()

    - by Rasmi Ranjan Nayak
    int main() { int *ptr, **ptr1; ptr = (int*)malloc(sizeof(int)); ptr1 = (int**)malloc(sizeof(int)); free(ptr); *ptr = 12345; ptr1 = &ptr; //free(ptr); //**ptr1 = 23456; printf("%d \n", **ptr1); system("pause"); return 0; } How does *ptr store the value 12345, when the memory has already been freed? So, now ptr should be pointing to garbage. Why is this happening?

    Read the article

  • Why does a non-constant offsetof expression work?

    - by Chris J. Kiick
    Why does this work: #include <sys/types.h> #include <stdio.h> #include <stddef.h> typedef struct x { int a; int b[128]; } x_t; int function(int i) { size_t a; a = offsetof(x_t, b[i]); return a; } int main(int argc, char **argv) { printf("%d\n", function(atoi(argv[1]))); } If I remember the definition of offsetof correctly, it's a compile time construct. Using 'i' as the array index results in a non-constant expression. I don't understand how the compiler can evaluate the expression at compile time. Why isn't this flagged as an error?

    Read the article

  • CUDA error message : unspecified launch failure

    - by user1297065
    I received the error message "unspecified launch failure" in following part. off_t *matches_position; ...... cudaMalloc ( (void **) &mat_position, sizeof(off_t)*10); ...... cudaMemcpy (mat_position, matches_position, sizeof(off_t)*10, cudaMemcpyHostToDevice ); ...... err=cudaMemcpy (matches_position, mat_position, sizeof(off_t)*10, cudaMemcpyDeviceToHost ); if(err!=cudaSuccess) { printf("\n3 %s\n", cudaGetErrorString(err)); } Do you know why this error message is reported??

    Read the article

< Previous Page | 55 56 57 58 59 60 61 62 63 64 65 66  | Next Page >