gerard4143 371 Nearly a Posting Maven

If I was creating this simple binary tree I would create my structure like so

struct tree_el 
{
  int *val;
  struct tree_el *right; 
  struct tree_el *left;
};

With an int pointer...That way you can test if the pointer exists with a simple statement like

struct tree_el *root = (struct tree_el *)malloc(sizeof(struct tree_el));

if (root->val)
{

}
gerard4143 371 Nearly a Posting Maven

You know this is written in C not C++...

gerard4143 371 Nearly a Posting Maven

You could use

p1->lastName
p1->firstname

gerard4143 371 Nearly a Posting Maven

You need to pass a pointer to a pointer for this to work...

Your function should be declared like.

void teste(int **p_int)

and you should pass your pointer like

teste(&m);

The reasoning is simple really. When you pass your pointer to a function it copies the pointer value not the address of the pointer.

gerard4143 371 Nearly a Posting Maven
CookieData* cookiedata = new CookieData(s,d,t,ipfile);
	void cookie = &cookiedata;
	setfunction(cookie);   //function where I want these values, cookie has to be a void pointer by function declaration

I'm pretty sure you want something like this:

CookieData* cookiedata = new CookieData(s,d,t,ipfile);
	void *cookie = cookiedata;
	setfunction(cookie);
gerard4143 371 Nearly a Posting Maven

You might to include just a bit more of the code...I have no idea what a obj is. Is it a variable, a reference to a variable, a pointer....

Your error. Did you try casting the void pointer to the appropriate type.

gerard4143 371 Nearly a Posting Maven

Remember that general rule...Call delete the same amount of times that you call new.

gerard4143 371 Nearly a Posting Maven

Well number one...you don't deallocate both since we're only talking about one chunk of dynamic memory.

In your above example you should delete one pointer to the memory chunk and then set both pointers to NULL.

gerard4143 371 Nearly a Posting Maven

The general rule is...If you call new x times then you must/should call delete x times.

gerard4143 371 Nearly a Posting Maven

You have to, at least, save the original pointer value somewhere so that you can delete and free up the memory...otherwise you create a memory leak.

gerard4143 371 Nearly a Posting Maven

I would put some std::cout's in here and check some values like degree

Poly Poly:: operator+(const Poly& p)
{
Poly temp;
for (int i=0; i<degree; i++){//look here...what's degree equal?
temp.coefs[i] =coefs[i] + p.coefs[i];
}
return temp;
}
gerard4143 371 Nearly a Posting Maven

Well the default constructor set degree to zero....the first element of the array coefs.

Poly Poly:: operator+(const Poly& p)
{
Poly temp;
for (int i=0; i<degree; i++){//look here...what's degree equal?
temp.coefs[i] =coefs[i] + p.coefs[i];
}
return temp;
}
gerard4143 371 Nearly a Posting Maven

A constructor with NO parameters is the default constructor. So for instance

Car a = new Car(1, "Bob", 10); //uses #1
Car b = new Car(1, 10, "Bob"); //uses #2 
Car c = new Car("Bob", 1, 10); //uses #3
Car d = new Car(); //uses #4 and calls the default constructor

Hope this helps.

Don't you mean Car *a = new Car()...

gerard4143 371 Nearly a Posting Maven

Try this to start out...

#include <string>

using namespace std;

class car 
{

      private:
               int yearModel;
               string make;
               int mpg;
      public:
               car();                  
};

car::car ()
{
       yearModel = 0;//zero is a good value to set numeric primary data types
       mpg = 0;//zero is a good value to set numeric primary data types
       //make string object will call its default constructor automatically 
}

int main()
{
	car mycar;
	return 0;
}
gerard4143 371 Nearly a Posting Maven

Try something like below

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

void mycall(char cmd[])
{
	char *pch = NULL;

	printf ("%s\n", cmd); /* correctly prints 'ls -l' */
	printf ("Before call to strtok\n");
	pch = strtok (cmd, " ");
	printf ("After call to strtok\n"); 
}

int main()
{

	char cmd[] = "ls -l";
	mycall(cmd);
	exit(EXIT_SUCCESS);
}
gerard4143 371 Nearly a Posting Maven

Quick question, does it have to be a vector? Could it be a map?

gerard4143 371 Nearly a Posting Maven

When you assign them memory or you may point then to an existing object which should already be initialized.

Your example 'class foo' returns len which does not exist....

gerard4143 371 Nearly a Posting Maven

On lines 11 and 12...Do you even have a valid constructor for these?

gerard4143 371 Nearly a Posting Maven

i see how i missed that. i fixed that, but this statement:

else if (command != "function1" || "function2")  
     {         
    cout << "Invalid Entry\n";       
      cout << "Please your correct terms\n";  
     }

keeps appearing. it always prints out, regeardless of putting y or Y.

Its the same problem as stated above by Akill 10

gerard4143 371 Nearly a Posting Maven

Yes use strtol()..

The value is the same, hex or integer, its just a matter of how you choose to display it.

gerard4143 371 Nearly a Posting Maven

Try solving this problem with a linked list. It turns out to be pretty simple...Here's a very rough and buggy example to show you what I mean.

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

struct node
{
	unsigned char value;
	struct node *nptr;
};

void showit(struct node *nptr)
{
	if (nptr->nptr) showit(nptr->nptr);
	fprintf(stdout, "%u", nptr->value);
}

void addone(struct node *ptr)
{
	if (ptr->value < 3)
	{
		++(ptr->value);
	}
	else if (!ptr->nptr)
	{
		ptr->value = 0;
		ptr->nptr = (struct node*)malloc(sizeof(struct node));
		ptr->nptr->value = 1;
		ptr->nptr->nptr = NULL;
	}
	else
	{
		ptr->value = 0;		
		addone(ptr->nptr);
	}
}

int main(int argc, char**argv)
{
	unsigned int i = 0;
	struct node start = {0, NULL};

	for (i = 0; i < 100; ++i)
	{
		addone(&start);
		showit(&start);
		fputs("\n", stdout);
	}
	exit(EXIT_SUCCESS);
}
gerard4143 371 Nearly a Posting Maven

This would be easier if you used strtol() to handle all the conversions.

gerard4143 371 Nearly a Posting Maven

Ok, I now have the following:

char *endp;
	long value1, value2;

        unsigned char *hexResult;

	char hex1[3] = "0x";
        char hex2[3] = "0x";

	strcat(hex1, inputData);
	value1 = strtol(hex1, &endp, 16);

	strcat(hex2, mask);
	value2 = strtol(hex2, &endp, 16);

Now value1 and value2 hold the decimal values for the two hex inputs. What I need to do is perform an AND operation on the two hex values when I get them into hex. E.g.,

0x1006 & 0xF0 = 0x1000

I know that hex values can be represented as integers. i.e. I can do something like:
int memAddr = 0x1fe;

And that I can perform an AND operation on two hex (int) values. How can this be achieved from the above?

Right away your character arrays are too small. You have

char hex1[3] = "0x";

and then you

strcat(hex1, inputData);

hex1 is only three characters long...i.e It already is filled with '0', 'x', '\0' so you can't append anything onto it without overflowing it.

gerard4143 371 Nearly a Posting Maven

Yes, I will read in a string something like "1006", and I want to change that to "0x1006", which I can then cast to an int to get the correct value (and perform an AND operation on that and another hex string; e.g., (0x1006 & 0xFF)). Does that make sense?

Yeah it does now...You'll have to convert(not cast) your string into an integer with strtol.

gerard4143 371 Nearly a Posting Maven

I'm not sure what you mean by a 'hex string'...Do you meant something like

char hex_string[] = "0xffddee99";

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

#define ARR_SIZE 6

int main(int argc, char**argv)
{
	char hex1[10] = "0x";
	char hex2[10] = "0x";

	char ch[ARR_SIZE];

	fputs("enter some hex->", stdout);
	fgets(ch, ARR_SIZE, stdin);

	strcat(hex1, ch);

	fprintf(stdout, "You entered->%s\n", hex1);

	/*
		now your working with a string of characters so you'll
		have to convert your string into a integer with strtol()
	*/

	exit(EXIT_SUCCESS);
}
gerard4143 371 Nearly a Posting Maven

what my code does is it waits for the user to type in "led" and performs the blink function. what i am trying to do is introduce the letter 'p' as a command to stop the blinking. what happens is that blink is only executed once.

if (!(strcmp(userinput, led))){		//condition 1
	for(howlong = 0; howlong++ < 0xFF; howlong++){		//condition 2	
		blink();
		if (getch() == 'p')
		break;
	}
}

getch() is probably picking up the '\n' in the input buffer...Try replacing if (getch() == 'p') with

fprintf(stdout, "char->%x\n", getch());

What's the value displayed?

gerard4143 371 Nearly a Posting Maven

I would try sum += lower...

int range(int lower, int higher, int sum=0)
{

 while (lower <= higher)
{
sum += lower;
++lower;
}

return sum;
}
gerard4143 371 Nearly a Posting Maven

You don't really have to compute the number of numbers...

while (lower <= higher)
{
//do something
++lower;
}
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

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

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 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

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

Try

int getc(FILE *stream);

gerard4143 371 Nearly a Posting Maven

try this

printf ("value : %u\n", *(unsigned char*)(p + i));

This will cast and dereference a byte starting at p + i...You had casting and dereferencing an int starting at p + i which will overflow past n = 0x000000ff.

gerard4143 371 Nearly a Posting Maven
char (&decision = "PASSED");

The above code is not allowed. If you need to compare strings then include the string.h header and use strcmp(). Ooops, I think you might be trying to set decision equal to "PASSED". If you want that to happen then change decision to a character pointer...

char *decision;

decision = "PASSED";
gerard4143 371 Nearly a Posting Maven

Why not just have an accumulator and keep a running total there.

sum = 0;
sum += getInt();
Don_k commented: FOR YOUR IMMEDIATE RESPONSE TO MY QUESTION FROM DON_K THANK YOU +1
gerard4143 371 Nearly a Posting Maven

I know that qt and gtk+ provide extensive documentation...plus you'll find numerous tutorials on the internet.

gerard4143 371 Nearly a Posting Maven
gerard4143 371 Nearly a Posting Maven

Sounds like homework to me...Lets see what you have so far.

gerard4143 371 Nearly a Posting Maven

Don't and I mean don't ever use

gets(myString);

Read Please
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

The parentheses are optional when applying sizeof to a variable.
p = malloc(sizeof words );

Now I didn't know that. I guess that's why I keep coming back.