gerard4143 371 Nearly a Posting Maven

By undefined behavior, do you mean that sometimes it can be changed and sometimes cannot be?

By undefined, I think she means not define within the C language so the compiler designer(s) are free to implement it any way they want....ie, don't expect consistent implementation.

gerard4143 371 Nearly a Posting Maven

i have no clue where or how to start, any idea would be immensely helpful..

If I was doing this 'but I'm not' I would investigate here

http://epaperpress.com/lexandyacc/

Redhaze46 commented: thanks:) +1
gerard4143 371 Nearly a Posting Maven

how hard?

Try making a parser first and then you'll start to see the 'how hard'.

gerard4143 371 Nearly a Posting Maven

I tried your code and got this

Aborted
[linux@localhost]$ echo $?
134
[linux@localhost]$

Note: I know that Ubuntu sets up its compiler slightly different than my Linux distro...

gerard4143 371 Nearly a Posting Maven

Depends...Where are you declaring this const variable? Is it local, on the read/write stack?

gerard4143 371 Nearly a Posting Maven

Thanks!

Don't let the relatively small size fool you -- Barbara and I packed a lot more information into this book than you will find in other books three times its size.

The downside of this strategy is that you have to read it carefully; you can't just skim.
Still, I think it's better to take the time to understand 350 pages than to bleep over 2/3 of 1,000 pages.

No worries, I'm a thorough reader...I read a chapter once to get a feel for the subject and then read it again for detail and then test my new skills with written code..I really look forward to reading your(well you and Barbara's) book...It has tremendous reviews on amazon.

gerard4143 371 Nearly a Posting Maven

Ok, i'll try strtol(), correct me if i'm wrong.

nptr is the string
what about (char**)NULL?
and that 10

You really should try Googling..What your asking is can be found by typing

c programming strtol

in your browser.

gerard4143 371 Nearly a Posting Maven

Here's what the man pages have to say:

int atoi(const char *nptr);

DESCRIPTION
The atoi() function converts the initial portion of the string pointed
to by nptr to int. The behavior is the same as

strtol(nptr, (char **) NULL, 10);

except that atoi() does not detect errors.

You really should write a small program and see what it returns.

gerard4143 371 Nearly a Posting Maven

GCC's linker supports alternate entry point names

ld - The GNU linker

-e entry
--entry=entry
Use entry as the explicit symbol for beginning execution of your
program, rather than the default entry point. If there is no
symbol named entry, the linker will try to parse entry as a number,
and use that as the entry address (the number will be interpreted
in base 10; you may use a leading 0x for base 16, or a leading 0
for base 8).

gerard4143 371 Nearly a Posting Maven

I'm biased, of course; but I think that Accelerated C++ is pretty close to what you're looking for; it definitely has a tutorial narrative, so it's intended to be read sequentially (unlike many that are essentially reference books) and it covers quite a bit more material than most introductory books.

I just got the biased part arkoenig

Accelerated C++: Practical Programming by Example by Andrew Koenig, Barbara E. Moo

Any relation?

Note I ordered the book this morning...

gerard4143 371 Nearly a Posting Maven

Depends on the operating system. QT is cross platform.

I'm not sure Qt has C bindings...You can check here.

http://qt.nokia.com/products/programming-language-support

gerard4143 371 Nearly a Posting Maven

You could try GTK+

http://www.gtk.org/

gerard4143 371 Nearly a Posting Maven

I'm biased, of course; but I think that Accelerated C++ is pretty close to what you're looking for; it definitely has a tutorial narrative, so it's intended to be read sequentially (unlike many that are essentially reference books) and it covers quite a bit more material than most introductory books.

Just to verify - You mean

Accelerated C++: Practical Programming by Example by Andrew Koenig, Barbara E. Moo

gerard4143 371 Nearly a Posting Maven

I would investigate the modulus operator.

gerard4143 371 Nearly a Posting Maven

Hi,

I'm looking for a book on C++...now before you flame me I did read the sticky "Read Me:C++ Books" and found the list interesting but for me uninformative...Why? I'm not really sure what C++ level I'm at currently. Let me elaborate...Here's my experience with the C family of languages..

I've been programming daily in the C language for about two years now, so I'm comfortable with the language but C++?? Well I did read/work through Sams 'Teach Yourself C++ in 21 Days' and have been picking at the language(C++) on again and off again for some time now but feel I need a book to take me to the next level(past beginner)..Do you know of one that doesn't spend too much time on the basics but has a nice continuity(beginner to intermediate...leaning more to intermediate) to it? Many thanks Gerard4143

gerard4143 371 Nearly a Posting Maven

Thanks for the reply Mike..I'm looking at your solution right now.

Note: I found a few typos but the code works..Thanks...You might want to verify the corrections.

#include <iostream>

//IO interface for all the classes:
class printable 
{
	public:
		//this pure virtual method will be called to print the derived class' object.
		virtual std::ostream& print(std::ostream& output) const = 0;
};

//with overloaded operators:
std::ostream& operator<<(std::ostream & out, const printable & rhs) 
{
	return rhs.print(out); //simply call the print method.
}


//then your own class hierarchy:
class animal : public printable
{
	public:
		animal(const char *name):itsname(name) {}
		virtual ~animal() {}

	//override the print function in each class that has any new data members.
		std::ostream& print(std::ostream& output) const 
		{
			return output << "animal's name->" << itsname;
		};
	protected:
		const char *itsname;
};

class mammal:public animal
{
	public:
		mammal(const char *name, unsigned long val):animal(name), itsweight(val) {}
		virtual ~mammal() {}

		//override the print function in each class that has any new data members (and call the base class method too).
		std::ostream& print(std::ostream & output) const 
		{
			return animal::print(output) << " - " << "mammal's weight->" << itsweight;
		};
	protected:
		unsigned long itsweight;
};
class Dog:public mammal
{
	public:
		Dog(const char *name, unsigned long weight, unsigned long age):mammal(name, weight), itsage(age) {}
		virtual ~Dog() {}

		//override the print function in each class that has any new data members (and call the base class method too).
		std::ostream& print(std::ostream & output) const 
		{
			return mammal::print(output) << " - " << "dog's age->" << itsage;
		};
	protected: …
gerard4143 371 Nearly a Posting Maven

Yes I did, and I showed you what you should have done. What you did made no sense.
Every animal has a name,weight, and age. Thus the animal class should contain all.
Instead of defining a operator<< for all three, you only need to define one operator<<.

So you see your structures does not really make sense. But to answer your question,
yes that is one way you can and did achieve what you wanted.

I want each object/class to define its own std::ostream operator<< and the derived objects to call/incorporate them into their own std::ostream operator<<. This way you can change the base classes std::ostream operator<< without changing the derived objects...Do you get what I'm driving at?

gerard4143 371 Nearly a Posting Maven

What you are doing is hard coding each stream insertion operator. This is what you want :

struct Animal{
 int weight, age;
 string name;
 Animal(string name = "", int weight = 0, int age = 0)
 : name(name), weight(weight), age(age){}
 ~Animal()=0{}
};
struct Mammal : Animal{
};
struct Dog  : Mammal{
};
std::ostream& operator>>(std::ostream& outStream, const Animal& animal){
 outStream << "Name : " <<  animal.name << "\n";
 outStream << "Weight : " << animal.weight << "\n";
 return outStream << "Age : " << animal.age << "\n";
}
int main(){
 Dog dog("Rudolph",150,7);
 cout << dog << endl;
}

Did you even read the posting? You hard coded the base objects std::osstream operator<< into the object dog...

gerard4143 371 Nearly a Posting Maven

Please check the line(s) of code after the comments '//this is the line of code in question... '.

I want to know if this is the proper way to use the std::ostream operator<< in multi-inheritance? Basically I want the derived object(dog) to call the base object's std::ostream operators without hard-coding it....Please note the program works, I'm just looking for a verification. Is this the proper way to use the std::ostream operator in this program?

#include <iostream>

class animal
{
  public:
    animal(const char *name):itsname(name) {}
    virtual ~animal() {}
    
    friend std::ostream& operator<<(std::ostream & out, const animal & rhs);
  protected:
    const char *itsname;
};
class mammal:public animal
{
  public:
    mammal(const char *name, unsigned long val):animal(name), itsweight(val) {}
    virtual ~mammal() {}
    
    friend std::ostream& operator<<(std::ostream & out, const mammal & rhs);
  protected:
    unsigned long itsweight;
};
class dog:public mammal
{
  public:
    dog(const char *name, unsigned long weight, unsigned long age):mammal(name, weight), itsage(age) {}
    virtual ~dog() {}
    
    friend std::ostream& operator<<(std::ostream & out, const dog & rhs);
  protected:
    unsigned long itsage;
};

std::ostream& operator<<(std::ostream & out, const animal & rhs)
{
  //this is the line of code in question...  
  out<<"animal's name->"<<rhs.itsname;
  return out;
}
std::ostream& operator<<(std::ostream & out, const mammal & rhs)
{
  //this is the line of code in question...
  out<<*dynamic_cast<const animal*>(&rhs)<<" - "<<"mammal's weight->"<<rhs.itsweight;
  return out;
}
std::ostream& operator<<(std::ostream & out, const dog & rhs)
{
  //this is the line of code in question...
  out<<*dynamic_cast<const mammal*>(&rhs)<<" - "<<"dog's age->"<<rhs.itsage;
  return out;
}

int main(int argc, char**argv)
{
  dog mydog("vicious dog woof", 4321, 34); …
gerard4143 371 Nearly a Posting Maven

Then I guess you should start programming right away...

gerard4143 371 Nearly a Posting Maven

Well me let introduce you to the C++ section...Its here

http://www.daniweb.com/forums/forum8.html

gerard4143 371 Nearly a Posting Maven
Ancient Dragon commented: Good point. I rewwrote it in C language. +33
gerard4143 371 Nearly a Posting Maven
cwarn23 commented: Great link and quick response. Thanks heaps. :) +5
gerard4143 371 Nearly a Posting Maven

Let's see what you have so far...

Question - Are you separating an integer into its separate digits or are you just putting spaces between something that's read from stdin?

gerard4143 371 Nearly a Posting Maven

I would read line by line and check the first seven characters for a match..Try the function

int strncmp(const char *s1, const char *s2, size_t n);

gerard4143 371 Nearly a Posting Maven

The braces(curly brackets) are there to define blocks of code and to help make your code readable...In some situations the coder can choose which he/she prefers but you should err on the side of readability.

gerard4143 371 Nearly a Posting Maven

I have been advised by many people that it is better to use integer return type for main function instead of void ! I actually dont understand what return 0 means .. main returns 0 value to program ! What does that mean ?! Could you please help me understand the concept?

void main()
{
body
}
int main()
{

body 
return 0;
}

By returning 0 or EXIT_SUCCESS in main the program indicates that it exited normally.
Why do we use this?

Consider if your C program is part of a shell script or batch file that executes in sequence with other programs..How do you know if your C program ran correctly? By checking the exit code.

gerard4143 371 Nearly a Posting Maven

Huh? Is this supposed to make sense?

Also, why are you using caps for your function? Is it a macro?

gerard4143 371 Nearly a Posting Maven

I never give people the address to my house.

I always give them a *copy* of the address to my house.

They always find my house, with no problem. ;)

When you post code, use [code] tags around your code - always.

You give them a copy of your address? Why?

gerard4143 371 Nearly a Posting Maven

This is C++, please post in the correct section.

gerard4143 371 Nearly a Posting Maven

Here's my two cents...I used the existing variable to reverse the characters.

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

char* reverseit(char* str, unsigned long size)
{
	unsigned long i = 0;
	unsigned char c;

	for (i = 0; i < (size / 2); ++i)
	{
		c = str[(size - 1)- i];
		str[(size - 1) - i] = str[i];
		str[i] = c;
	}

	return str;
}

int main(int argc, char**argv)
{
	if (argv[1])
		fprintf(stdout, "%s\n", reverseit(argv[1], strlen(argv[1])));
	
	exit(EXIT_SUCCESS);
}
gerard4143 371 Nearly a Posting Maven

I did mention that classes were involved. Apologies, the only reason I posted in the C forum is that I have posted in C++ forums before and have been that I should post in the C forums (plus, even though I mention that I can only use C library functions, people still tend to post their C++ solutions).

If your talking about member functions then its a C++ solution.

gerard4143 371 Nearly a Posting Maven

Yes, that works fine if I create my function without the class specifier. However, using

int CTSL1_Commands::TSL1_ReadMemory32 (char *sCmdInt[], char returnData[256])

I'm still getting the same errors. Any ideas?
I need the class specifier as i'm calling this function from another class

pTSL1_Common->TSL1_ReadMemory32(...)

Where did the class specifier come from? This is the first time you mentioned it! If your trying to implement a class function, then this should be posted in the C++ section.

gerard4143 371 Nearly a Posting Maven

Well I tried this and it works.

#include <stdio.h>

typedef int (*myfunction)(char *[], char[]);

int TSL1_ReadMemory32 (char *sCmdInt[], char returnData[256])
{
	fputs(sCmdInt[0], stdout);
	fputs(returnData, stdout);
	return 12;
}

struct 	
{	
    myfunction thefunc;	
    const char* functionName;
} 
lookUpTable[] = { {&TSL1_ReadMemory32, "ReadMemory32"}, {NULL, NULL} };

int main(int argc, char**argv)
{
	return lookUpTable[0].thefunc(argv, "test data\n");
}

Maybe if you should include this functionality in a separate C source file and then compile as C code. Once compiled try linking it into the main C++ program....Note I never tried mixed C/C++ programming on a Windows environment.

gerard4143 371 Nearly a Posting Maven

You mention that you have a class in your code. Does this code have a mixture of C and C++?

gerard4143 371 Nearly a Posting Maven

Well right away your function definition is different than your structure function definition...

int TSL1_ReadMemory32 (char *sCmdInt[], char returnData[256]);

compared to

void (*function)(void);

gerard4143 371 Nearly a Posting Maven

Try compiling the small program I posted. Did it compile and execute correctly?

gerard4143 371 Nearly a Posting Maven

gerard4143:

Why are you suggesting a typedef for the function? I am not suggesting that that you are wrong; I am just interested in what you see as the advantages and disadvantages are of the typedef over direct declaration:

#include <stdio.h>

void TSL1_ReadMemory32(void)
{
	fputs("called!\n", stdout);
}


struct 
{
	void (*function)(void);
	const char* functionName;
}lookUpTable[] = { {TSL1_ReadMemory32, "ReadMemory32"}, {NULL, NULL} };


int main() 
{
	lookUpTable[0].thefunct();
	
	return 0;
}

Do you generally define a typedef for all your types? (If so, that answers the question.)

I find the code cleaner and easier to read with a typedef...Personal preference.

gerard4143 371 Nearly a Posting Maven

Try something like

#include <stdio.h>

typedef void (*myfunction)(void);

void TSL1_ReadMemory32()
{
	fputs("called!\n", stdout);
}


struct 
{
	myfunction thefunct;
	const char* functionName;
}lookUpTable[] = { {TSL1_ReadMemory32, "ReadMemory32"}, {NULL, NULL} };


int main() 
{
	lookUpTable[0].thefunct();
	
	return 0;
}
gerard4143 371 Nearly a Posting Maven

How is MOLNUMBER defined?

gerard4143 371 Nearly a Posting Maven

Maybe you should try running the code and see what happens

gerard4143 371 Nearly a Posting Maven

It's my fault, I didn't completely understand his post. I think what he wants to do is have ptr point to the fifth element in the array and then have y point to the forth one, by subtracting element 1 from element 5. In this case, the following code applies

#include<stdio.h>
     
int main(void) {
     
    int x[] = {1, 4, 8, 5, 1, 4};
    int *ptr, y;
    ptr = x + 4;
    y = ptr - 1;
     
    printf("ptr: %d\n", *ptr);
    printf("y: %d\n", *(x + y));
     
    return 0;
}

Now to try to explain things a bit, y is a simple integer variable (not a pointer), which stores the result of subtracting 1 from the index of ptr . The original version was redundant because x = x[0] , and ptr = x[4] . Therefore, we have x[4] - x[0] , which, in pointer arithmetic, means 4 - 0, which is redundant. But you want to go one element before the one ptr points to, so you have to subtract 1 from its index. The bug here is that arrays are indexed from 0 to N-1, where N is the size of the array. Therefore, if you try to subtract the first element's index, you fall into redundancy. So the only way around this is to subtract one, as I did in the above code. Next, the *(x + y) part in the last printf(); actually means x[y] , so that's how you do something very simple with pointers and pointer arithmetic.

I …

gerard4143 371 Nearly a Posting Maven

Well... first of all, it should not come out 8, but 5.

Going into your code, y is not defined as a pointer, but it should, because what you are performing inside your code is pointer arithmetic.

Next, you are subtracting the address of the array, as x actually means &x[0] . So what you want to do is y = ptr - *x .

Performing the modifications, we end up with

#include<stdio.h>

int main(void) {

    int x[] = {1, 4, 8, 5, 1, 4};
    int *ptr, *y;
    ptr = x + 4;
    y = ptr - *x;

    printf("ptr: %d\n", *ptr);
    printf("y: %d\n", *y);

    return 0;
}

It outputs

ptr: 1
y: 5

Your line 8 should be:
y = ptr - x;

gerard4143 371 Nearly a Posting Maven

Try substituting a memory value for x and working out the values...It works out to 4.

gerard4143 371 Nearly a Posting Maven

Maybe I'm missing your point.

If I was to write this I would have the client request a country by name from the server. The server in response to the client request would:

1. Read the country's name from the client.
2. Open the file containing the country data(I'll assume its collected in one file)
3. Search file for a country name match.
4. If match is found send the matched line else send a generic error message.

gerard4143 371 Nearly a Posting Maven

Right away I can see that your server is not reading the request from the client. So how does your server get the country value or does it send everything with each request?

gerard4143 371 Nearly a Posting Maven

Here's the return value of strmp

RETURN VALUE
The strcmp() and strncmp() functions return an integer less than, equal
to, or greater than zero if s1 (or the first n bytes thereof) is found,
respectively, to be less than, to match, or be greater than s2.

The ! operator negates the return value of strcmp.

gerard4143 371 Nearly a Posting Maven

Thanks faxo and ancient dragon. Ancient Dragon i tried out your code however no value was printed out. Faxo, I also think its a good idea to use the fgets. However it should have been just gets since i'm not dealing with files over here.

Not sure what you mean by 'should have been just gets since i'm not dealing with files over here'...It really doesn't matter, gets(char*) is dangerous...

BUGS
Never use gets(). Because it is impossible to tell without knowing the
data in advance how many characters gets() will read, and because
gets() will continue to store characters past the end of the buffer, it
is extremely dangerous to use. It has been used to break computer
security. Use fgets() instead.

gerard4143 371 Nearly a Posting Maven

If you interested in 'low level' and kernel programming then may I recommend picking up a book on inline assembler.

http://ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html

Plus I would investigate several books on kernel/operating system programming.

gerard4143 371 Nearly a Posting Maven

Try

int getc(FILE *stream);