Tuesday, July 14, 2009

C code problem?

#include%26lt;stdio.h%26gt;


int calsum(int x,int y, int z);


main()


{


int a,b,c, sum;


printf("\n Enter any three numbers");


scanf("%d %d %d",%26amp;a, %26amp;b, %26amp;c);


sum = calsum(a,b,c);


printf("\nSum=%d",sum);


}


int calsum(int x,int y,int z)


int x,y,z,;





{


int d;


d=x+y+z;


return (d);


}


error messages is


function should return a value in function main()


declaration syntax error


deceleration terminated incorrectly


Plz tell me where I am wrong

C code problem?
#include%26lt;stdio.h%26gt;


int calsum(int x,int y, int z); **** write int calsum(int, int, int);


main()


{


int a,b,c, sum;


printf("\n Enter any three numbers");


scanf("%d %d %d",%26amp;a, %26amp;b, %26amp;c);


sum = calsum(a,b,c);


printf("\nSum=%d",sum);


}


int calsum(int x,int y,int z)


int x,y,z,; ****remove this





{


int d;


d=x+y+z;


return (d); ***use return d;


}





Please try then tell me if u still encounter error
Reply:main() should be void main()








replace the main() by void main()
Reply:return 0;





in main
Reply:The main method returns an integer value by default. Since in your code, no value is returned by the main(), the error occurs.





You may either choose to define main as





void main()


{


// your code goes here


}





or,





main()


{


// your code





return 0;


}
Reply:type return 0; in the last line in the main program. surely this will solve this problem

daffodil

Is that a program that will automatically generate a header file from my c programming source code(not C++).?

I am programming a c language linked list stack in a program called Eclipse. I have the source code in stack.c. I am looking for a program that will scan my stack.c source code and generate a stack.h header file automatically.





Please let me know if there is anything like that out there and a quick explanation of how to use. it.





Thank You

Is that a program that will automatically generate a header file from my c programming source code(not C++).?
no its program that takes few codes from header files and use it


C code question regarding malloc() and free().....?

file ----%26gt;input.h (contents of input.h)


float * datain();


file ----%26gt;input.c(contents of input.c)


#include %26lt;malloc.h%26gt;


//.....other includes


float *datain()


{


float *data;


//routine to get data........


return(data);


free (data) ///????????


}





assuming I have a main.c function in the same project which calls datain()...is it legal operation to free the pointer after returning the pointer to the main function? even though I need to do some thing to the returned data?





file----------%26gt;main.c





#include %26lt;malloc.h%26gt; ....//other includes


#include "input.h"





void main ()


{


float *getdata;


getdata = datain();





// .....do something to get data;





}








Thanks in advance

C code question regarding malloc() and free().....?
statements after return statement are not executed.


so basically, your function does not execute the line


%26gt; free(data)





instead of that, you could define a structure and two functions,





typedef struct {


float * data;


int valid;


} datastruct;





datastruct get_data(){


float *tmp;


// malloc(tmp)





// copy data to memory pointed by tmp





datastruct result = {tmp, 1};


}





float free_data(datastruct *pdata){


if (pdata -%26gt; valid){


pdata -%26gt; valid = 0;


free (pdata -%26gt; data);


}


}





==================





and inside your main function:





void main(){


datastruct data;


data = get_data();





// do smth to data





free_data(%26amp;data);


}
Reply:you're right, the struct is not really needed. Report It

Reply:Actually in C different memory space will be alloted for the functions so whenever u pass a data the value will be passed but when it is a pointer it only passes the address of the pointer so if u pass a pointer from one function to another the address of the pointer will be passed.





If u want to free a pointer the u need to pass the data value instead of using address of the pointer


ie instead of return(data); use return(*data); It passes the data value so when u free the location u dont have any problem





If u free the pointer only the pointer will be freed ie if u free data then it will be deleted (the location having the address of original data )as u have already passed the address if u free there is no problem .if u free first the original address of data will be lost thus the data becomes inaccessible .








I think i have atleast solved some of your problems
Reply:I really didn't use C before ... but i know the pointers concept.





I think there's no problem if u make the pointer "data" free after returning the data


even if u need to do things to the returned data in the main.





----


*u've pointer "data" in dataIn() and pointer "getData" in the main,


*what is really done here is that u recieved some DATA


*the DATA now is in the memory with address "150" for example


*u put the address "150" in the pointer "data"


*u returned the pointer value to the pointer "getData"


*then "getData" now contains the address "150"





i don't know the function "free" but,





when u free(data):





-if that means u make the pointer itself free and not points any DATA, so u still have the pointer "getData" points to the same data.





-if it means u'll delete the actual DATA pointed by the pointer "data", then that's wrong





but it seems like the 1st meaning is right


----------------


another thing :


as i said b4, i didn't use C before ... but in Java, the statements after "return" aren't reachable as the function ends at the word "return", i don't know if it's the same thing in C or not
Reply:[747] cmalloc: cat main1.c


#include %26lt;stdio.h%26gt;


#include %26lt;stdlib.h%26gt;





float *dataIn( void )


{


float *dataPtr = malloc( sizeof(float) );





*dataPtr = 1.2345;


return dataPtr;


}





int main( int argc, char* argv[] )


{


float *myDataPtr = dataIn();





printf( "myDataPtr: %p\n", myDataPtr );


printf( "*myDataPtr: %f\n", *myDataPtr );





free( myDataPtr );


return 0;


}


[748] cmalloc: gcc -Wall main1.c


[749] cmalloc:


[749] cmalloc: ./a.out


myDataPtr: 0x3000f0


*myDataPtr: 1.234500








The dataIn() function allocates the memory, fills it in, and returns a pointer to the data. The main() function gets a pointer to the data from the dataIn() function. The ownership of the allocated memory passes from the dataIn() function to main(). The main() does whatever it wants with the data and then frees the memory.





You must NOT free the memory until all the program is completely done using the data stored within the memory.


C# code help?

Hello. I am trying to get a program to insert the date at where current text is being typed (not explained very well, I mean the little line when you type name is on the tip of my tongue...). At first I tried to get it to appear using the following code





[code]


rchTextCode = rchTextCode.txt + DateTime.Today;


[/code]





But it only added it at the end of the text





I am sure I am doing something really obviously wrong, any help?

C# code help?
I work with VB but looking at MSDN it seems similar....so try...





rchTextCode.SelectedText = rchTextCode.SelectedText + DateTime.Today;
Reply:use the selectionstart property to determine the


location of the cursor/caret.





richTextBox1.Text=


richTextBox1.Text.Insert


(richTextBox1.SelectionStart , "hello world");


Please suggest a tool for debugging memory leak in C++ source code?

Hi... we are providing software support for one of the famous retailers in U.S. The code has been written in C++. Our field test customers feel that the application software is very slow.


Before we deliver the software to our clients, we are in a urge to improve the performance of the application. Some of our software engineers suspect that the memory dirty pages are too high in the application. They feel that the code have lot of memory leaks. Are there ways to improve the performance of the software?? Is there any tool that finds us the memory leaks in the code? Please suggest?

Please suggest a tool for debugging memory leak in C++ source code?
For MSVC, we sometimes use BoundsChecker--decent tool. The wiki page on BoundsChecker will give you other vendors.





http://en.wikipedia.org/wiki/BoundsCheck...
Reply:Hi, typically, a memory leak occurs because dynamically allocated memory has become unreachable. You can prevent memory leaks by watching for some common problems. Collection classes, such as hash tables and vectors, are common places to find the cause of a memory leak. This is particularly true if the class has been declared static and exists for the life of the application. . The prevalence of memory leak bugs has led to the development of a number of debugging tools to detect unreachable memory. Coverity Prevent is one of the tool that you can use for fixing these kinds of bugs. Coverity Prevent is also used by the Department of Homeland security to scan many open source projects. You can get more info at http://www.Coverity.com
Reply:Use purify.

hyacinth

How to write DllImport kernel32 in C++.net unmanaged code?

Can someone tell me syntax for importing SetEnvironmentVariable from kernel32.dll in C++ unmanaged code.

How to write DllImport kernel32 in C++.net unmanaged code?
check out http://www.pscode.com - site with good examples
Reply:Wow...I have no clue what you're talking about. LOL.


I'm searching for a c++ source code that does Quine McCluskey reduction?

Hello, I'm Sudesh. I hope you guys can do me a favour. I'm searching for a


C++ source code that does Quine McCluskey reduction. I've been searching the


net for it but can't seem to find it. I hope there is someone here who can


help.

I'm searching for a c++ source code that does Quine McCluskey reduction?
There may not exist a C++ solution on the web, but I Googled "Quine McCluskey reduction" and got quite a few results. I believe at least a few could be helpful for your purposes... In fact, I found one result in C that looked very promising -which I'll provide below...
Reply:You're quite welcome... Report It

Reply:http://en.wikipedia.org/wiki/Quine-McClu...





Coding it based on this description should be a reasonably easy task for a C++ programmer such as yourself.





Rawlyn.