Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You can contribute to threads even if you don't know the answer to the original question. If you find a thread where someone answers it and you don't understand the answer, then go ahead and ask for more clarification about what you don't understand. But be careful here not to hijack the thread and take it in some other direction. If you don't understand some answers then chances are there are other readers who don't either. That kind of dialog is one way for everyone to learn something new that day :)

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>None.
That link doesn't work for me.

>>there is a pointer passed there.
The problem is not the pointer -- the problem is typecasting int* into char* when passting its value to printf(). int* is four bytes of memory, while char* is only one. So *(char*)p1 tells the compiler to pass only the first byte of that four-byte integer to printf(). It may actually seem to work when storing the value of 10 to the integer. But store the value of 123456 to the integer and see what you get.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Aha..
If I may say, that would be stupid.. :)

My point exactly.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>> Sorry for annoyance. Is VC++ 2010 Express compliant?
Yes. What compiler are you using?

>>I had an impression we're not talking pointers in general
That was referring to the printf() line, where the format specifer string uses %d yet only one of the 4 bytes of the integer is in the parameter list.

As for the malloc() line -- it had notthing to do with endianness.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The code you posted doesn't have any typcasting problems. Try this and see what gcc does: long* pl1 = (char *)p1; That's the kind of typcast problem I'm talking about.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>Now I am really confused. Which snippet? int* x = (char *)malloc(sizeof(int)); >>Care to elaborate? (TM)
Do I really need to explain what's wrong with the above code snippet? Put that line in a C program and try to compile it. VC++ 2010 Express gives warning: warning C4133: 'initializing' : incompatible types - from 'char *' to 'int *'

>>It will be getting int all right.The pointer is very well aligned. On a little-endian machine it would be a right int, surprisingly.
Not really a pointer problem. Try storing a large number in the integer (someing greater than 255) then see what you get. It takes 4 bytes (on 32-bit compilers) to store an integer. All that is being sent is one of those four bytes.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

See the code snippet I posted for clarification of what I meant. Of course int* can be typecast to char* under some circumstances. But not when typecasting the return value of malloc. Another place where such typecasts won't work correctly is line 6 of the original post in this thread. In that case its not a compiler error, but a logic error because, as someone already pointed out, printf() will be expecting an integer but only getting a char which will be upgraded to int when its passed to printf().

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Confused as always. malloc returns void * , which of course can be typecasted to anything.

.

I didn't say void* can't be typecast to anything. I said it int* can't be typecast to char*. int* x = (char *)malloc(sizeof(int)); Please read and comprehend before you look like a fool.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

could not is correct -- meaning that if you did it then the compiler should probably produce an error for wrong typecast. (can not convert char* to int*, or something like that)

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>> Yes the program is open source but I dont wanna compile

How do you expect to accomplish the goals you posted if you don't recompile the program???? :icon_eek:

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

In C language the return value from malloc() does not need to be typecast at all. But if you do use typecast then it needs to be typecast to the appropriate type. For example the line 3 you posted the return value of malloc could not be typecast as char* because that it the wrong type.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you want to rename main.h to myclass.h so that the name of the file tells you what's in the file. Other than that, there is little, if any, difference between the two methods. It's all a matter of programming style. I prefer the first method when one header file depends on another.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

There's nothing magic about what I posted. Just a simple loop that adds endl to the end of all but the last line. Instead of an loop of 10, and it a loop of 9. After the loop finishes the last line is saved without endl.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Oops. That was probably my fault for posting that bad code.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Compare the code you posted with the code I posted. They are not the same.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

What do you need help with? We can't help you if you can't tell us.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>Ok, so time() function (argument being null) returns what?

It returns the number of seconds since 1970, as you previously posted. It doesn't matter whether the parameter is NULL or not, it will always return the same value.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Post what you tried because you must have made a mistake.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

DLLs written in C++ can not be used by non-c++ languages unless you make the functions look like C functions by adding extern "C" to them, which prevents the compiler from mangling function names. In otherwords you will have to write C wrapper functions around the c++ code in the DLL.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Here is a Microsoft article you will want to study. I'm not sure that C# can access DLLs written by non-.NET languages.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

There's a couple ways to avoid storing that extra '\n' at the end of the text file. One way is like this:

ofstream myfile("Stock.txt");
if (myfile.is_open())
{
   for(int i=0; i <8; i++)				 
      myfile << number[i] <<endl;
   myfile << number[i];
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Consider the source. Your comment was completely on the mark and justified and you got down-voted precisely BECAUSE it was completely on the mark and justified. Upvoted from me as a counterbalance.

Ditto

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Since updateDifference() is a void function you can't set a variable = to it. The function is going to change the value of totalOfDifferences because it's passes by reference. Example below.

updateDifference(cashPrice, creditPrice, totalOfDifferences);
cout << " prices is now R " << totalOfDifferences << endl;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The way to solve it is not to put that function call on the same line as the cout statement. Put that function call statement on its own line before that cout line so that the cout and display the new values of those variables.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

That's what I thought. Its a void function. How do you expect cout to print the return value of a function that doesn't return anything???

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>updateDifference(cashPrice, creditPrice, totalOfDifferences)

What does that function return?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You failed to include <iostream> and <string> and the using std::cin; , using std:cout; and using std::string; statements.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>if (nights<='30') && (nights!='0')

Badly formed. You need more parentheses. And a single character can not hold more than one numeric digit, so probably need to test against integers instead of charcters. if ((nights<=30) && (nights!=0))

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The code I posted does not change the pointers, only the values to which they point. If you want to use xor then do so.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

swapping two integers in assembly is a lot easier than the code you posted. This assumes 32-bit integers on Intel family of 32-bit/64-bit processors

mov esi,x_ptr
mov eax,[esi]
mov edi,y_ptr
mov edx,[edi]
mov [esi],edx
mov [edi],eax
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>avg = ass_average(int num_students, int j);

When you call a function you do not put the variable types as part of the parameters. That's only for function prototypes. All you have to do it put the variable names as the parameters, like this

avg = ass_average(num_students, j);

cdn88 commented: Thank you for such a quick replay, that fixed it! +0
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

dev-c++ is no longer recommended because its ancient, lots of bugs, and no longer supported.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

vc++ 2010 express or Code::Blocks. Both are good C and C++ compilers with good debuggers. I prefer VC++, but for portability between MS-Windows and *nix you might want to do with Code::Blocks.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
int* a = new int[4]; // allocate memory for 4 integers
a[0] = GetParentVar1();
a[1] = GetParentVar2();
a[2] = GetParentVar3();
a[3] = GetParentVar4();
return a;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You can compile C code as c++ with no problems. All win32 api functions are C, none of them are pure c++.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>> Shouldn't there would be a way to detect uninitialized variable

Maybe there should, but there isn't. There are no invalid values in C or C++ lanauges. And isNULL will not return any valid information because there is no way to detect whether an integer contains valid data or not. You can only guess.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Have you studied this tutorial?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

There is no such animal as a NULL integer in either C or C++ languages to indicate that the integer has no value. There are languages that have that concept, but not in C or C++. C compilers will often produce either warnings or errors if you try to assign NULL to an integer because NULL may be defined as (void *)0 which is a pointer.

As previously mentioned C and C++ compilers initialize global variableds to 0. Other variables are left uninitialized so your program has to initialize them.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

In the old days of MS-DOS that was pretty easy -- the program just kept track of which ports were in use. My MS-DOS program had up to 40 comm ports. You can still use that approach but also have to be aware that other processes may open a port. So if CreateFile() fails to open the port then your program must assume that the port is in use by some other process. I haven't used multi-port boards on MS-Windows operating systems, but I assume the manufacturer supplies a device driver, and you will have to read their documentation for details on how to use it.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Yeah, so? You compile C-version-6 with the compiler C-version-5 and you have an updated compiler.

And that's the Chick & Egg dilemma. What did K&R use the build the first C compiler (see the answer here)?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

m a bit confused...how can it b written in C coz again u will need a C compiler to compile it..
also if it is written in assembly..then how can the code run on different machines..i mean,assembly language would be different for different architectures,won't it..??

printf() is not a built-in function, but instead it is a library function, and all library functions are compiled with the C compiler. C compilers do not have any built-in "functions".

How library functions are written is up to whoever wrote the compiler, or is implementation dependent. Microsoft may write it in C language, but Borland might write it in assembly. Microsoft has actually done it both ways with various compilers.

Its true that assembly language functions restrict their use to only one architecture. Such fucntions have to be completly rewritten to port to others.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

mkdir() will create a directory. google for mkdir() to get more information.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

use Windows communications functions. You can't access the ports directly as you did in MS-DOS. Instead, you have to open the com port using CreateFile(), then set up the port parameters with SetCommConfig(). After that you use ReadFile() and WriteFile() to read and write to the com port. For ReadFile() I put that in another thread so that it doesn't block the entire program.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>Well, then what is the point of being NULL? Why set it to zero?

In c++ NULL is just a macro (see this link if you don't know what a macro is) that is defined to be 0. Normally NULL is used to initialize pointers to a known address so that the pointer is never confused with some valid address. NULL means nothing at all when used with integers because 0 is a valid integer value. So it makes no sense to assign NULL to an integer. c and c++ languages do not have any way to tell us that an integer is uninitialized, or has no value at all.

iamcreasy commented: Awesome, exactly what I was looking for. +2
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

try disabling any anti-virus software you may have running.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

line 12: how is variable c defined? fgetc() returns int, not char.

You need to post the code that write the file. Also attach a copy of the file or post the first few lines if the file is text.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The first parameter is not char** but char*. Delete key1 and key2 because neither
are needed. And the last parameter is a pointer to your comparison function, which will be something like the following.

Is variable catFreeEntry the number of elements in the catalog array of structures?

int compare(void* k1, void* k2)
{
   struct catPage* p1 = (struct catPage *)k1;
   struct catPage* p2 = (struct catPage *)k2;

   return strcmp( p1->????, p2->????); // compare the two structures
}

bsearch(key, catalogue, catFreeEntry, sizeof(struct catPage), compare);

Also note that the first argument to bsearch needs to be of the same type as the second argument, in your program the first argument needs to be struct catEntry. So you need to copy key into a struct catEntry so that you can pass it as the first argument. The reason for doing that is because bsearch() will pass it to the comparison function.
Read more details about bsearch() here

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>strcpy(address,NULL);

strcpy() hates that statement because it expects a valid pointer as the second argument, not NULL pointer. If all you want to do is initialize the array to 0 then use memset(), e.g. memset(address,0,sizeof(address)); , assuming that address is not a pointer. Or you can just simply do this: address[0] = 0;

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I think pipes will work for you. Here are a few links you might want to read.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

why don't you ask Facebook???