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

I think the longest string that can be made in C or C++ is the maximum value of an integer (as defined by size_t). Compilers will concantinate string liters just by NOT using ';' between lines. Note the use of quotes below The semicolon ; only appears at the end of the string.

char big_string[] = "ljasd lakdjfl lkjdf"
"lkjsdf alkdjf"
"kjhsdfkgh kjhdfg";
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Sometimes there is a difference in C, but not in C++. C compilers often define NULL as (void *)0 or (char *)0, but all standard C++ compliant compilers define NULL as 0. For that reason, NULL is never (supposed) to be applied to integers, just pointers.

int n = NULL; <<< WRONG
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you are trying to do it the hard way. fgets() and strtok() functions that make it very simple. fgets() reads a whole line, and strtok() splits it into its individual parts based on a token, such as a space, that you give it.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

int main(int argc, char* argv[])
{
	char buf[255];
	FILE* in = fopen("infile.txt","r");
	if(in == NULL)
	{
		printf("file not found\n");
		return -1;
	}
	FILE* out = fopen("outfile.txt","w");
	while( fgets(buf,sizeof(buf),in) != 0 )
	{
		char *ptr = strtok(buf, " ");
		while(ptr != 0)
		{
			fprintf(out,"%s ", ptr);
			ptr = strtok(0," ");
		}
	}
	fclose(in);
	fclose(out);
	
	return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

just because 0 is a null pointer constant doesn't mean it points to address 0.

You are splitting hairs -- the memory location of a pointer contains an address. A NULL pointer ALWAYS has absolute address 0 in C and C++ programs. Its up to the implementation to evaluate that address as appropriate for the operating systems. There are many references to that, here's just one of them, including Bjarne Stroustrup

http://www.eskimo.com/~scs/C-faq/q5.2.html

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

A NULL pointer is a pointer that points to address 0

const void *p = 0;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

c and c++ compilers do not do range checking on enumerations or pointers. It lets you hang yourself with a very very long rope if you want to. Its up to the programmer to validate data ranges and buffer sizes before using or doing something with them. Those are two very good reasons for c++ programs to use the STL classes.

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

stdio.h contains function prototypes (declarations) and structure definitions from the standard C stream library. You cann't write either a C or C++ that uses any of those functions without including stdio.h. If the program doesn't use anything from it, then there is no need to include it.

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

get it from the keyboard as a string instead of an integer, then create a loop to check each digit in the string. You don't need modules, division, shifts, or any other math operations, unless, of course, that is a requirement of the problem.

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

argc is the number of command-line arguments, argv is an array of strings. Each argument on the command line is separated by one or more spaces, so if you type this in a cmd-prompt

c:>myprog -a something -b something else

The above will cause myprog.exe to receive 6 arguments
argv[0] == always the name of the program, such as myprog.exe
argv[1] == "-a"
argv[2] == "something"
argv[3] == "-b"
argv[4] == "something"
argv[5] == "else"

The arguments can also be passed when one program executes (spawns) another program using functions such as the spawn family of functions (man for them). or in MS-Windows CreateProcess() win32 api function.

Those arguments can get somewhat complicated. *nix compiler often have functions to help -- such as getopt(). I know of no equlivant function in M$ compilers :(

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

may compile, but calling the constructor like that is unnecessary because it is called when the object is instantiated. So doing it like that is the same as calling a function twice. In some cases it might even crash the program, depending on what the constructor does.

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

But there should be some way.
If we can use pointer in the place of array is it possible
Ashwin Perti

You are writng c or c++ program NOT fortran. If you want the compiler to do high-level math then use a fortran compiler, which will PROBABLY do what you want (I don't know and don't care! :) ). When you write c and c++ code there are many things the compiler does NOT do for you -- you have to do them youself or get libraries where someone else did all the work for you.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
A(j);

How is that we are calling the constructor explicitly...i thought it was not possible

It isn't, and that's one of the reasons that VC++ 6.0 won't compile the program. Turbo C++ compiled it because it is just a plain crappy compiler that no self-respecting c++ programmer would admit to using :mrgreen:

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

I don't really understand the question -- but there is no magical way to multiply two arrays. c++ does not support matrix algegra like you would see in a math class. You have to code the loops yourself, unless you want to buy (beg, borrow or seal :cheesy: ) it from somebody else.

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

Your first example is ALMOST how to multiply two arrays. A[100] does not exist in an array with 100 elements. you have to create a loop and multiply each array element in array A by the corresponding element in array B. Then the result has to be stored someplace, either in array C, or back in A or B. Afterall, if the result isn't stored someplace there is no point to doing the math.

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

you shouldn't call the constructor directly like in function show(). Do that in a set function. This works ok in VC++ 6.0 compiler. Nearly all c++ programs use set functions similar to below -- none use private constructors to do it.

#include<iostream>
using namespace std;

class A
{
private:
	int a;
	float b;
public:
	A()
	{
		cout<<"\n Default constructor invoked\n";
		a=0;
		b=0;
	}

	A(int j,float i)
	{
		a=j;
		b=i;
		cout<<"\n value "<<a<<" and "<<b<<endl;
	};
	void SetA(int j)
	{
		a = j;
		cout << "a = " << a << endl;
	}
public:
	void show()
	{
		int j;
		cout<<"\n Enter value of j ";
		cin>>j;
		SetA(j);
		cout<<"\n new values are "<<a<<" and "<<b << endl;
	};
};
int main(int argc, char* argv[])
{
	A a1;
	A a3(8,4.01F);
	a1.show();
	a3.show();
	return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

argv[0] will give you the same as GetModuleFileName(). XP has windir environment variable. Earlier os may be different -- check your PATH environment variable.

char* path = getenv("windir");

I'm supprised the return pointer from getenv() is not const because you can't modifiy its value.

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

do you want the drive letter of the drive on which the os is installed? such as "c:"? or the path that the program is starting from? argv[0] or win3w2 api function GetModuleFileName() will give you the latter.

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

you didn't do anything "wrong". There are a lot of porting issues you must deal with when porting C programs to C++. Most (if not all) the errors you are getting are probably because the functions have not been prototyped before used in main(). You need the *.h file that prototypes all the functions in that library.

If the program is already including the correct header files, make sure the prototypes contain the correct parameter list -- c++ is picky about that. For example, c++ initialize_fold(int) is not the same as initialize_fold()

Second issue: when you get everything to compile correct, it may not link with that C library. C++ normally "mangles" function names, while C does not. To keep c++ from doing that so that the program can be linked with C libraries you have to declare the functions as Extern "C". Here is an example -- you will find many other examples in your c++ compiler's standard include directory.

#ifdef _cplusplus // if compiled with C++ compiler.  Note that this is not
// standard, check your compiler's docs and other header files to find out
// the correct macro it uses for c++ versus C compilations.
extern "C" {
#endif
//
// put C function prototypes here
int intialize_fold(int);
// other function prototypes here


#ifdef _cplusplus // if compiled with C++ compiler
} // end of extern "C" block
#endif
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Both The Codes Works Fine.....Now What??Why Memory Allocation

There are many many cases where you do not know the text that the character array contains. For example, suppose I ask you for your name. The program has to put the answer some place, if the pointer doesn't point to some value memory locations then your program will crash big time.

// allocate space to put your name, up to 79 characters
char *name = malloc(80);
// display the prompt
printf("Enter your name");
// get name from keyboard
fgets(name,80,stdin);

the above could also be done like this

// allocate space to put your name, up to 79 characters
char name[80];
// display the prompt
printf("Enter your name");
// get name from keyboard
fgets(name,80,stdin);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
int bar(const void *a, const void *b)
{
   const int *x = a, *y = b;
   return (*x > *y) - (*x < *y);
}

That will not compile with either VC++ 6.0 or Dev-C++. Requires cast like this

int bar(const void *a, const void *b)
{
   const int *x = (int*)a, *y = (int*)b;
   return (*x > *y) - (*x < *y);
}

but otherwise an interesting algorithm.

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

Oh! your right Dave -- because subtracting a minus is really addition. I knew that, just checking if you did too :cheesy: :o

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

but remember that strcmp expects pointers to const char, not pointers to char.

that's one of the purposes of function prototypes, to give the compiler enough information to make correct data type conversions. So you always do this? (I don't)

char name1[20];
char name2[20];

...
if( strcmp( (const char*)name1, (const char*)name2))
...

I never explicitly typecast the parameters to const char*. So why bother in that comparison function I posted ?

And the same thing applies to all functions that take a const something-or-another.

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

What's INT_MAX - INT_MIN?

-1 with my compiler -- but you'r right about int overvlow in that one and only case. I don't ever recall any number of real world applications where that will happen, but then I havn't read all real-world programs either :D

Right, you are incorrectly casting away the constness. Casting with (const int*) would be better.

Ok now I see your objection. Easy fix to the purests, but does not really do anything in this situation. But I suppose one should be consistent and use const correctly as you pointed out.

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

now how can subtracting two integers possibly cause integer overflow?

Narue: I agree your solution is simpler. But don't thumb you nose at nested conditional statements just because you may not understand them. Eeeeew :lol:

Dave: I'm sertain you already know that the two parameters must be cast to the correct type. So I don't know what you meant in your post. I could have posted this, but others, such as Narue, may not have understood it

return *(int *)p2 - *(int *)p1;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

How difficult the program is to port all depends on the program. If the program uses strickly ANSI C or C++ functions there should be few if any porting problems. But most programs use some hardware-specific functions. You will just have to take them one at a time. First attempt to compile with the compiler on MAC computer then fix up whatever errors that compiler spits out. In some cases you might need to do major rewrite of the program, or parts of the program. I know little, if anything, about MAC, so can't really help you any more. I do know, however, end-of-line terminator on MAC file system is not the same as MS-Windows, MS-DOS or *nix. So if your program has hardcoded "\n" or "\r\n", then you will have to change it to "\r". If the program used a macro for eol sequence then that port will be trivel.

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

look for qsort() -- all you have to do is write your own custom comparison function that returns an integer similar to strcmp(). The code below assums you are sorting an int array in descending order.

int mycomp(const void* p1, const void* p2)
{
    int* i1 = (int*)p1;
    int* i2 = (int *)p2;
    return *i2 - *i1;
}

Or an array of strings, in descending order (I think)

int mycomp(const void* p1, const void* p2)
{
    int x =  strcmp((char*)p1,(char*)p2);
    return (x < 0) ? 1 : (x > 0) ? -1 : 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Are P, Q, A and B individual data elements? If they are then the first

a. (P*Q) + (Q*P) + (R*S*T) + (R*S*T) + (T*R*S)

I don't know the significance of the ' character, so the above may not be completly correct.

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

This will work -- the function must return char* which is a pointer just like the parameter. But there is no point passing the parameter if the function is going to change and return the pointer.

char* outin(char *string,bool *bl)
{
    
     if (bl==1){
            string = "in";
            }
     else if (bl==0) {
          string = "out";
          }
     return string;
}

This would be more logical. And why are you passing the bool by reference instead of by value? The function isn't changing the value so you might as well pass by value.

char* outin(bool bl)
{
    char* string = 0;
     if (bl==1){
            string = "in";
            }
     else if (bl==0) {
          string = "out";
          }
     return string;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

yes.

char * foo()
{
   static char array[] = "Hello World";
   return array;
}

int* foo1()
{
   static int array[20];
   return array;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

get a free compiler from www.bloodshed.net. See sticky for books

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
o << num1.read_list(i);

function read_list() returns void, so the above line will not compile.

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

malloc() must be typecast when using a c++ compiler (file extension is *.cpp or *.cc), C compilers do not have that restriction.

char *array2 = reinterpret_cast<char*>(malloc( strlen(array[0]) +1 ));

Since you are writing c++ code, if you have the choice use std::string instead -- its a lot simpler.

std::string array2 = array[0];
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you can use malloc() to allocate the space needed to hold the characters

char *array2 = malloc( strlen(array[0]) +1 );
strcpy(array2, array[0]);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

something like this will hold an unlimited number of strings that are of unlimited length.

const char *array[] = {
 "jane",
 "John Deer Company",
 "The quick brown fox jumped over the lazy dog",
 "Adam",
 "Smith"
};
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

see the <limits.h>, which defines the max value of all integer types. Allocations cannot exceed those values.

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

Thanks, but I didn't even make it an application of either type to begin with. I just opened up new source files each time, and saved them as a .h and .cpp files.

Well, if you are going to use Dev-C++ then do it right. Create a project and then add the code to it.

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

put the dll in a place where the os can find it, one of the directories in the PATH or in the same directory as the program you are trying to run.

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

you probably created the wrong kind of project. You should have created a Console Application, not a Windows Application. With a Console Application your program compiled with no errors or warnings. Of course I had to add a main() function to the code you posted.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
CString str;
unsigned short test = 0x1234;
str.Format("%04X", test);
CString n1 = str.Left(2);
CString n2 = str.Right(2);
// now put it back
str = n1 + " " + n2;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Thanks -- it works great :)

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

Is there a c++ std:: function that will trim trailing spaces from std::string? I know how to do it myself, but I was wondering if anyone knows of something in <altorithms> or elsewhere?

Thanks

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

Another problem will be the fonts used at your terminal. Every terminal may be using a different font set, so the spacing between colums will be diffeent. This app may be a good use for curses library, or use a good GUI library such as QT.

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

not quite certain what you want. Do you want to convert the unsigned short to CString? CString's Format function is identical to sprintf() and printf().

CString str;
unsigned short test = 0x1234;
str.Format("%X", test);

Of course if you display the result, the string will be "1234".

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

sounds like the function names in the dll have been mangled, so mathlib doesn't know how to handle that. In the dll, use extern "C" if you don't need c++ classes to keep the compiler from mangling the names.

extern "C"
{
   void _dllexport foo();
}

DllMain() is called by the os, not directly by using applications. Its sortof like a c++ class constructor/destructor in that it is called then the DLL is loaded into memory and again just before exit.

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

CodeProject has at least one that you might find useful.

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

in VC++ 6.0 IDE create a "win32 Dynamic-Link Library" project. Step 1 of the wizard has 3 radiao buttons select "a DLL project that exports some symbols" then press Finish button. The wizard will generate the DLL project. Look at the *.cpp and *.h files. They will show you how to add your own functions and how to "export" them so that they can be used by other applications. Just copy-past the functions or files into that project, export them as illusterated.

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

seems to work with c++ STL classes too ;)

#pragma warning(disable: 4786)
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main()
{
	vector<string> array;
	array.resize(2);
	char* p1 = (char *)&array[0];
	char* p2 = (char *)&array[1];
	cout << p2-p1 << endl;
	cout << sizeof(array[0]) << endl;
	return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

This works with Dev-C++ but not with VC++ 6.0 (I think it is a compiler bug)

HWND hWnd = GetConsoleWindow();
ShowWindow(hWnd,SW_SHOWMAXIMIZED);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Ah! Ha! I overlooked that -- I have a temp and tmp environment variables.