gerard4143 371 Nearly a Posting Maven

Save lots of RAM? Not really.

Here's a union example to find the byte order of a system.

#include <iostream>

int main(int argc, char**argv)
{
	union
	{
		short s;
		char c[sizeof(short)];	
	} un;

	un.s = 0x0102;

	if (sizeof(short) == 2)
	{
		if ( un.c[0] == 1 && un.c[1] == 2 )
			std::cout << "Big endian" << std::endl;
		else if (  un.c[0] == 2 && un.c[1] == 1  )
			std::cout << "Little endian" << std::endl;
	} 
	return 0;
}
gerard4143 371 Nearly a Posting Maven

Your kidding right? You want us to read the docs to you?

gerard4143 371 Nearly a Posting Maven

I would check the multimap container.

http://www.cplusplus.com/reference/stl/multimap/

gerard4143 371 Nearly a Posting Maven

Not sure why your handling your pointer that way? Try writing your program like below:

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

#define ELEM_POS 2

int main(int argc, char** argv)
{
    int x[] = {1, 2, 3, 4};
    char *p = (char*)x;

    fprintf(stdout, "p->%d\n", *(int*)p);

    p = (char*)(p + sizeof(int) * ELEM_POS);

    fprintf(stdout, "p->%d\n", *(int*)p);
    return (EXIT_SUCCESS);
}
gerard4143 371 Nearly a Posting Maven

You may be wondering why two + operators are required? Try reading this link, it may clear up some of your confusion.

http://www.bogotobogo.com/cplusplus/this_pointer.html

I should clarify that Mike 2k's solution doesn't require this explanation because both + operators are defined outside of the class Number but if you had a class something like below:

#include <iostream>

class Number
{

public:

    Number():itsvalue(0) {}
    Number(int val):itsvalue(val) {}

    Number operator +(const Number & a) const { return itsvalue + a.itsvalue; }

    int getitsvalue() const { return itsvalue; }

private:

    int itsvalue;

};

Number operator +(const int val, const Number & n) { return val + n.getitsvalue(); }

std::ostream& operator <<(std::ostream & out, const Number & n)
{
    return out << n.getitsvalue();
}

int main(int argc, char** argv)
{
    int x = 123;
    Number my_n(789);

    std::cout << x + my_n << std::endl;
    std::cout << my_n + x << std::endl;
    return 0;
}

Then the explanation does make sense.

gerard4143 371 Nearly a Posting Maven

Here's a simple example that'll work with a ascii text file.

#include <iostream>
#include <fstream>

struct mys
{
	char ca[10];
	unsigned long a, b;
} thes;

std::ifstream& operator >>(std::ifstream & in, struct mys & s)
{
	in >> s.ca;
	in >> s.a;
	in >> s.b;
	return in;
}

int main(int argc, char**argv)
{
	std::string filename;
	std::cout << "Enter filename->";
	std::cin >> filename;

	std::ifstream myfile (filename.c_str());

	myfile >> thes;

	std::cout << "ca->" << thes.ca << " a->" << thes.a << " b->" << thes.b << std::endl;
	
	myfile.close();
	
	return 0;
} 

/* data file

one 44 45
two 34 87
three 56 87
four 34 56


*/
gerard4143 371 Nearly a Posting Maven

Do you require a closing brace on line 962(a closing brace after the break on line 962)?

gerard4143 371 Nearly a Posting Maven

Pointers are data variables that store(that should store) NULL or a valid memory address. Pretty simple right?

gerard4143 371 Nearly a Posting Maven

i think the problem is imba using linux so characters extended ascii characters are 16 bits i need tips on where will i start im kinda lost right now on what to do

Are you referring to wide characters or extended ascii characters?

gerard4143 371 Nearly a Posting Maven

Extended ascii characters are 8 bits, see link below

http://telecom.tbi.net/asc-ibm.html

Try reading your characters into an unsigned char variable.

gerard4143 371 Nearly a Posting Maven

Try something like below

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

unsigned long *lptr = NULL;

int main()
{
  lptr = (unsigned long*)malloc(sizeof(unsigned long));
  
  /*check if allocation failed*/
  
  __asm__ 	(
				"movq	%0, %%rax\n\t"
				"movq	$6, (%%rax)\n\t"
				:"=m"(lptr)
			);
			
  fprintf(stdout, "ans->%lu\n", *lptr);
  return 0;
}
wildplace commented: =D great helpl +2
gerard4143 371 Nearly a Posting Maven

For this to work, you'll have to inspect each character from the stream using the isdigit() function.

gerard4143 371 Nearly a Posting Maven

I literately typed this in google

c++ number to string

and it returned itoa().

http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/

gerard4143 371 Nearly a Posting Maven

Take the '\n' out of your scanf format specifiers.

gerard4143 371 Nearly a Posting Maven

Could you be more specific? What do you mean by 'system'?

gerard4143 371 Nearly a Posting Maven

Try adding to the top of your make file

all: list.o listinterface.o
gerard4143 371 Nearly a Posting Maven

Your call to atoi may be failing.

From my help file

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.

Try using strtol() instead.

gerard4143 371 Nearly a Posting Maven

Yes, GTK+ has event handling and its written in C.

gerard4143 371 Nearly a Posting Maven

It should work if irSource is derived from inputSource and if irSource is not abstract.

gerard4143 371 Nearly a Posting Maven

You can't create abstract objects, hence the term abstract. You can create a pointer to an abstract type but its must point to a derived object.

Here's a simple coded example

#include <iostream>

class expr_node//abstract
{
public:

    virtual ~expr_node() {}

    virtual std::ostream& print(std::ostream & out) const = 0;

};

std::ostream& operator << (std::ostream & out, const expr_node & e)
{
    return e.print(out);
}

class int_node: public expr_node//derived
{
public:

    virtual ~int_node() {}

    std::ostream& print(std::ostream &out) const { out << "derived"; }
};

int main(int argc, char**argv)
{
    expr_node *me[4];

    for (int i = 0; i < 4; ++i)
        me[i] = new int_node;//creating pointer to derived type
    return 0;
}
gerard4143 371 Nearly a Posting Maven

The strcmp() function requires c-strings which must be terminated with '\0'. What your passing to strcmp() is a char pointer to one character.

c-strings

char *str1 = "this is a c string";//compiler adds the '\0'
char str2[] = "this is another c string";//again the compiler adds the '\0'
char str3[] = {'a','b','c','\0'};//another c string
char str4[] = {'a','b','c'};//not a c string no terminating '\0'
gerard4143 371 Nearly a Posting Maven

Thanks for the reply, Dragon. Unfortunately, I think the problem is somewhere else. I've included the whole code here:

http://www.daniweb.com/software-development/cpp/threads/357691/1525051#post1525051

That code is C++, its not C.

gerard4143 371 Nearly a Posting Maven

The operating system will use the next lowest file descriptor for that application. It really makes sense if you think about it. Would you expect the operating system to return a random number?

The third value is used if open creates a new file, its the umask value that's applied to the newly created file.

vedro-compota commented: +++++++++++ +1
gerard4143 371 Nearly a Posting Maven

Try compiling this program

testp.c

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

int main(int argc, char**argv)
{
  char ch;
  FILE *fd = NULL;
  
  if ( argc != 2 )
  {
	fputs("usage error - a.out <Filename>\n", stderr);
	exit(EXIT_FAILURE);
  }
  
  if (!(fd = fopen(argv[1], "r")))
  {
	fprintf(stdout, "Could not open %s\n", argv[1]);
	exit(EXIT_FAILURE);
  }
  
  while (fread(&ch, sizeof(char), 1, fd))
  {
	fprintf(stdout, "%p ", (void*)ch);
  }
  
  fclose(fd);
  return 0;
}

Now pass a file name as a command line argument..It should display the binary(hex values) contents of the file.

gerard4143 371 Nearly a Posting Maven

Let me state, as I have before, your assuming that the constant element is writable.

gerard4143 371 Nearly a Posting Maven

Really, why would you want to? You know that some of the binary characters have no ascii representation, hence can't be displayed. It would be better to display each binary value in hex, thus displaying each and everyone of them.

gerard4143 371 Nearly a Posting Maven

Sorry I am too late. The right way is indeed to use a constructor like this:

struct foo { const int value; };
struct relaxed_foo { int value; };
struct foo * init_foo(int value) {
    struct relaxed_foo * rc = malloc(sizeof(struct foo));
    rc->value = value;
    return (struct foo *) rc;
}

The struct relaxed_foo shall be kept internal to the implementation.

and how is that real?

gerard4143 371 Nearly a Posting Maven

Of course. I do not expect it to be compliant; the Standard pretty much says that const qualification forbids the qualifiee (for a lack of the better word) to be a modifiable lvalue. It even makes it viral, in a sense of 6.3.2.1-1, which already makes allocation in your original snippet noncompliant. If I am right in my interpretation, my proposal wouldn't do any more harm.

In C++ the solution is simple with the use of constructors but I don't think you can allocate and initialize constants within the C framework or at least I can't find any way. Thanks for the input.

gerard4143 371 Nearly a Posting Maven
#include <stddef.h>
...
    * (int *) (((char *) sptr) + offsetof(struct mys, value)) = 1234;

Nice solution but it still assumes that the const int can be hacked around.

gerard4143 371 Nearly a Posting Maven

I don't know how to initialize it, but try this and your solution doesn't work. My suggestion is not to put const variables inside structures. It wouldn't make any sense to put them inside a structure anyway because by definition you set a const variable to a single value which does not depend on any given instance of the structure. const variables have the same value for all instances of the structure and can never be legally changed. Of course you can typecast out the const and change it that way, but that's cheating.

struct mys
{
    char buf[255];
    const int value;
};

Oh, I'm aware of the limitations of that solution. That's the primary reason for posting the question.

gerard4143 371 Nearly a Posting Maven

Try this link

http://www.daniweb.com/software-development/cpp/threads/350132

Didn't realize this was C++, replace std::cout with fputs().

gerard4143 371 Nearly a Posting Maven

How do you initialize a dynamic structure that has a const data member? I can provide an ugly hack but I'm at a loss as how to do it correctly. Actually, can you do this in a platform independent way?

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

struct mys
{
  const int value;
};

int main(int argc, char**argv)
{
  struct mys *sptr = (struct mys*)malloc(sizeof(struct mys));
  
  *(int*)sptr = 1234;
  
  fprintf(stdout, "ans->%d\n", sptr->value);
  
  /*sptr->value = 67;*/
  return 0;
}
gerard4143 371 Nearly a Posting Maven

Why not define your function like below?

void printTable(char tacToe[][3], int row, int col)
{

}

void initTable(char tacToe[][3], int row, int col)
{

}

int main(void)
{
    char tacToe[3][3];
    initTable(tacToe, 3, 3);
    printTable(tacToe, 3, 3);
	
	return 0;
}
gerard4143 371 Nearly a Posting Maven

Your reading 100 digits, that's one big number. Its even bigger than an int.

A safer conversion function would be strtol(), atoi() does not detect errors.

gerard4143 371 Nearly a Posting Maven

I just tried that but I didnt help. But am I right assuming that it->first is a const int due to the iterator being constant and you wanted to match parameters?

I was mistaken, because the function

std::vector<passenger*>& passenger_queue::passengers_at_floor(int floor_)

makes a copy of the const data member first, its allowed.

gerard4143 371 Nearly a Posting Maven

It could be this function

std::vector<passenger*>& passenger_queue::passengers_at_floor(int floor_)

Try changing it to

std::vector<passenger*>& passenger_queue::passengers_at_floor(const int floor_)
gerard4143 371 Nearly a Posting Maven

Try reading the last section of this page

http://pw1.netcom.com/~tjensen/ptr/ch8x.htm

gerard4143 371 Nearly a Posting Maven

If the rows of data are consistent, why not create a structure to represent a row and have an array of structures.

gerard4143 371 Nearly a Posting Maven
uint16_t _inverse_logic:1;

This creates a variable _inverse_logic which is one bit in size.

gerard4143 371 Nearly a Posting Maven

im asking is there any additional overhead for the first case? i dont see how u can instantiate an object without any member variables :(

Both classes will produce objects with this pointers. The this pointer is just a pointer to the member data. In the case where the class contains no member data, the implementation will instantiate a token about of memory and point this at that.

gerard4143 371 Nearly a Posting Maven

In the second case, you can call the static function without an object.

gerard4143 371 Nearly a Posting Maven

I say this in the most constructive way, learn how to program in C and then learn how to program client/server applications and then learn the HTTP protocol.

Line 8 is wrong.

gerard4143 371 Nearly a Posting Maven

i am writing a web server thats serves one html file but when i go to my browser and type in localhost:50000/home/user/www/index.html i get something like this

viewd on my browser

heres my code for writing to a socket

n = read(connectfd, buffer, 255 );
	 	if (n < 0) error("ERROR reading from socket");
	 	 printf("Here is the message: %s\n",buffer);
	 	  FILE *open = NULL;
	 	  char err_msg[] = "Could not open";
	 	  
	while((open = fopen("/home/rocco/ww/home.html","r") != EOF))
	{
		n=write(connectfd, &open,1);
		}
			
}

To write a web server, you have to be intimately familiar with the HTTP protocol and how to write client/server applications. I would suggest, learning TCP/IP programming and then concentrate on the HTTP protocol.

gerard4143 371 Nearly a Posting Maven

all i want to do is when i type in localhost at the browser a html page is served

i want to know how does the browser works does it request a filename from the browser ? or does the server just serves the index page when the browser has connected ?

Basically, you pass a URL(Uniform Resource Locator) to a web server which uses this to find the resource...http://en.wikipedia.org/wiki/Uniform_Resource_Locator

The code I posted, does it work? Can you request and get the text file contents?

gerard4143 371 Nearly a Posting Maven

@moroccanplaya

Gerard is opening the file in r mode while you are opening the file in rb mode. Could this be an issue ?

I'm pretty sure Linux doesn't distinguish between binary and text files.

The mode string can also include the letter 'b' either as a last
character or as a character between the characters in any of the
two-character strings described above. This is strictly for com‐
patibility with C89 and has no effect; the 'b' is ignored on all
POSIX conforming systems, including Linux. (Other systems may
treat text files and binary files differently, and adding the 'b'
may be a good idea if you do I/O to a binary file and expect that
your program may be ported to non-Unix environments.)

gerard4143 371 Nearly a Posting Maven

Uhhh I've never compile with a makefile before... I download codeblocks-mingw10 because someone said itll work.

I just dont know the steps to take.. I know I need to use command prompt and the cd command and the make command.. but Other than that, Im completely lost.

Open a console and go to the folder that contains Makefile and type

make windows

gerard4143 371 Nearly a Posting Maven

This will compile on both windows and Linux but you'll need the mingw32 compiler for windows and the GCC compiler for Linux.

gerard4143 371 Nearly a Posting Maven

I would read up on pointers and arrays in general, try this link

http://pw1.netcom.com/~tjensen/ptr/pointers.htm

gerard4143 371 Nearly a Posting Maven

Its not about answers, its about guidance. Please post the code that you have completed so far.

gerard4143 371 Nearly a Posting Maven

Did you allocated the memory? Try something like below.

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

#define STR_SIZE 35/*don't use magic number use descriptive labels*/
#define ARR_SIZE 6

int main()
{
  int i = 0;
  char *p[ARR_SIZE];

  for (i = 0; i < ARR_SIZE; ++i)
	p[i] = (char*)malloc(STR_SIZE);/*allocate memory*/
  
  for(i = 0; i < ARR_SIZE; ++i)
  {
	fputs("Enter a string->", stdout);
	fgets(p[i], STR_SIZE, stdin);/*never use gets use fgets, see comments below*/
  }
  for (i = 0; i < ARR_SIZE; ++i)
  {
	printf(p[i]);
  }
  
  for (i = 0; i < ARR_SIZE; ++i)
	free(p[i]);/*free memory*/
  return 0;
}

/*
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.
*/

Also, when you post code please make sure its format correctly.