gerard4143 371 Nearly a Posting Maven

Yeah picked up Accelerated C++ and i'm trying to get through it.
The name is apt though, about 20 new ideas every chapter. Hard to follow(still trying though).

Accelerated C++ paves the way for books like - The C++ Standard Library A Tutorial and Reference

gerard4143 371 Nearly a Posting Maven

Also line 14

char* subExprssn;

You use this without allocating or assigning memory to it.

gerard4143 371 Nearly a Posting Maven

Line 11 and 12...should you allocate the memory for the node first before you you allocate the memory for one of its members.

gerard4143 371 Nearly a Posting Maven

I tried rewriting you code...Please see this link on passing multi-dim arrays in C

http://www.eskimo.com/~scs/cclass/int/sx9a.html

#include <stdio.h>


void print_matrix(int n, int m[4][4]); 

void print_vector(int n, int v[4]); 

void matrix_x_vector(int n, int y[4], int x[4][4], int A[4][1]);


int main()

{  

	int n = 4;



	int m1[4] = {1, 2, 3, 4};


	int m2[4][4] = 	{

				{1,2,3,4},

				{5,6,7,8},

				{9,10,11,12},

				{13,14,15,16}

			};


	int multiplied_matrix[4][1] = 	{

						{0},

						{0},

						{0},

						{0}

					};





	print_vector(n, m1);

	print_matrix(n, m2);

	matrix_x_vector(n, m1, m2, multiplied_matrix);



	printf("\n");
	return 0;

}



void print_matrix(int n, int m[4][4])

{

	int i, j; 

	printf("\nMatrix Given\n");



	for (i=0; i<n; i++)

	{

		for (j=0; j<n; j++)

			printf("%3i", m[i][j]);

		printf("\n");

	}

}



void print_vector(int n, int v[4])

{

	int i;

	printf("\nVector Given\n");



	for (i=0; i<n; i++)

		printf("%3i", v[i]);



	printf("\n");

}



void matrix_x_vector(int n, int y[4], int x[4][4], int A[4][1])

{

	int i, j;



	printf("\nResulted Matrix of [M]*[V]\n");





	for (i=0; i<n; i++)

	{

		for (j=0; j<n; j++)

		{

			A[j][0] += x[j][i] * y[i];

			printf("%4i", A[j][0]);

		}

	
		printf("\n");

	}



	printf("\n");





	for (i=0; i<n; i++)

		printf("%4i\n", A[i][0]);

}

I never verified if the values were correct..

Caeon commented: Very helpful. Helped with solution and gave a link for more info on the topic. +1
gerard4143 371 Nearly a Posting Maven

Number one, your defining your functions inside of main..Place them before main like

void print_matrix(int n, int m[n][n]); // Prints an nxn matrix
void print_vector(int n, int v[n]); // Prints a vector of n elements
void matrix_x_vector(int n, int y[n], int x[n][n], int A[n][n]);

int main()
{}
gerard4143 371 Nearly a Posting Maven

Or like so if you want to use main's parameters

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



int main(int argc,  char **argv)
{
	if (argc == 2)
	{
		int i = 0;
		int size = strtol(argv[1], NULL, 10);
		int mya[size];		

		for (i = 0; i < size; ++i)
			mya[i] = 10 + i;

		for (i = 0; i < size; ++i)
			fprintf(stdout, "ans->%d\n", mya[i]);
	}

	return 0;
}
gerard4143 371 Nearly a Posting Maven

Try setting up your VLA in a function call like so

#include <stdio.h>

void myfunc(int size)
{
	int i = 0;
	int mya[size];

	for (i = 0; i < size; ++i)
		mya[i] = i + 10;

	for (i = 0; i < size; ++i)
		fprintf(stdout, "ans->%d\n", mya[i]);
}

int main(int argc,  char **argv)
{
	myfunc(12);
	return 0;
}
gerard4143 371 Nearly a Posting Maven

stdout and stdin are part of the standard streams.

gerard4143 371 Nearly a Posting Maven

Why don't you scan them in as characters e.g.

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

int main()
{
	char ch[3];
	unsigned long myint = 0;
	ch[2] = '\0';

	fscanf(stdin, "%c%c", &ch[0],&ch[1]);

	myint = strtol(ch, NULL, 10);

	fprintf(stdout, "ans->%lu\n", myint);
	return 0;
}

Note this is something you 'never learned' so please don't past it off as your own...Bonus marks should be earned.

WaltP commented: That input is really lame. Why use the bloated fscanf instead of the simple fgetc? -3
gerard4143 371 Nearly a Posting Maven

Your example above may make perfect sense to you but I'm having difficulties understanding what it means.

gerard4143 371 Nearly a Posting Maven

Do you mean how large of a value? If you do then check the limits.h library

gerard4143 371 Nearly a Posting Maven

This should answer some of your questions..Note I didn't know what an animal was so I used an integer array..

#include <iostream>

void myfunc2(const int & x)
{
  std::cout << x << std::endl;
}

void myfunc(void)
{
  int *mya[3];
  mya[0] = new int(123);
  mya[1] = new int(456);
  mya[2] =  new int(789);
  
  myfunc2(*mya[0]);
  myfunc2(*mya[1]);
  myfunc2(*mya[2]);
  
  delete mya[0];
  delete mya[1];
  delete mya[2];
  
}

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

linux and gcc

As A.D. said you'll have to provide more info. These programs, are they on the same machine or distributed over a network? Will you be using pipes, shared memory, sockets or even possibility threads?

gerard4143 371 Nearly a Posting Maven

If you passing it as a value or reference then that's different..The easiest way is to pass it by value...

Re-write your member function like so:

void  Animal::receive(int age)
   {cout<< age <<endl;}

and call it like so:

if (a < 10) { ani.receive( arry[i]->age);

Please note you allocated memory for your arry elements but didn't free them...

gerard4143 371 Nearly a Posting Maven

That's because arry is out of scope. in the member function

void Animal::receive(const Animal &){...}

gerard4143 371 Nearly a Posting Maven

You should be able to use

array[1]->age;

or

(*array[1]).age;

gerard4143 371 Nearly a Posting Maven

thanks will have a look.

You should check out the recommended C++ book's sticky

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

I've read Accelerated C++ and The C++ Standard Library A Tutorial and Reference and both are excellent books.

gerard4143 371 Nearly a Posting Maven

If you really want to understand function overloading them check this link out

http://en.wikipedia.org/wiki/Name_mangling

gerard4143 371 Nearly a Posting Maven

Posting every query that pops into your head is a misuse of this forum.

gerard4143 371 Nearly a Posting Maven

What exactly don't you understand?

gerard4143 371 Nearly a Posting Maven

If the server has created the shared memory then pass the shared memory id, passing pointer values between address spaces is wrong. With the shared memory id the clients can attach to the shared memory...

gerard4143 371 Nearly a Posting Maven

Why does it seg fault? Because you have to send the value that the pointer points to and not the pointer.

int *iptr = (int*)malloc(sizeof(int));
*iptr = 1234;

/*the wrong way*/ 
write(sock_var, iptr, sizeof(iptr));

/*the right way*/
write(sock_var, *iptr, sizeof(int));

Note - pointers are only valid in their own address space.

A few questions...This client server program, do they reside on the same computer and do they use Unix sockets? Because if they do you should be passing values used in functions like shmat() which requires shared memory id....once you attach, your pointing to the shared memory.

gerard4143 371 Nearly a Posting Maven

This has so many unanswered questions....

How does the listening part of the server know that no more clients are pending without waiting?

You mentioned shared memory segments at first then you say memory segment, which is it? Does the listening server create one memory segment for all the clients? Does the listening server create one segment for each client or do you have some other scheme?

gerard4143 371 Nearly a Posting Maven

Because 'a' is a character with the value 97(in the ascii codes its 97)..."a" is a c-string that has the characters 'a' and '\0'. The last character('\0') is added by the compiler.

gerard4143 371 Nearly a Posting Maven

Number one, what is campus? Is it a c-string pointer or a character..Your comparing it it to a c-string pointer and them assigning a character to it.

So what is campus?

gerard4143 371 Nearly a Posting Maven

You don't need to loop through the nodes in your overload << operator....Just print the current one and then point to the next node to print.

gerard4143 371 Nearly a Posting Maven

Yes, we haven't learned how to use the string libary... Would this make my life easier? Because this whole function thing is not making sense to me.

Using the string.h library would indeed make life easier but would invalidate the exercise..The idea is to understand, at least superficially, how to create this type of functionality.

gerard4143 371 Nearly a Posting Maven

I made the classic mistake of == instead of =.
Sorry!!
:(

Happens to all of us. Thank-you for posting that you found the solution.

gerard4143 371 Nearly a Posting Maven

On line 6

ICommDlgBrowser3 : public ICommDlgBrowser2

Your compiler can't find a definition of ICommDlgBrowser2

Did you include the proper header file?

gerard4143 371 Nearly a Posting Maven

Thanks, your help was very appreciated!

Your code did not help me much though, I guess I still had a problem to define the problem more precise as I did not now were the problem was. The code you gave me is working but not in my environment. Today I suddenly remembered the "strcpy()" function, and now it is working. :)

OK, its not an access problem but a copying a c-string over problem. Remember to allocated memory for your structure pointer before you copy the c-string data over.

gerard4143 371 Nearly a Posting Maven

Well I always use fstream in my includes for file I/O.

gerard4143 371 Nearly a Posting Maven

Well the first thing I would get working is a function that finds a c-string in a c-string. Once that's working then you can work on removing the characters.

A side note - Are trying to accomplish this without the string library?

gerard4143 371 Nearly a Posting Maven

Your defining your static structure as global...So why can't you access it? Try this

#include <stdio.h>

typedef char * msg;

static struct queue
{
  msg message;
  struct queue * next;
}myQueue;


void b()
{
  printf("b()->%p, %s\n",(void*)myQueue.message, myQueue.message);
}

void a(msg m)
{
  myQueue.message = m;
  printf("a()->%p, %s\n",(void*)myQueue.message, myQueue.message);
}

int main()
{
	a("this is the string");
	b();
	return 0;
}
gerard4143 371 Nearly a Posting Maven

I think I understand(I'm probably wrong but you gave so little information). This will return the address of the static structure so that you can access the msg member..Note this breaks the reason for having local static variables.

#include <stdio.h>

typedef char * msg;

void** myfunc()
{
	static struct queue
	{
		msg message;
		struct queue * next;
	}myQueue;

	fprintf(stdout, "msg->%s\n", myQueue.message);

	return (void**)&myQueue;
}

int main()
{
	void **vptr = myfunc();

	*vptr = (void*)"this is some string";

	myfunc();
	return 0;
}
gerard4143 371 Nearly a Posting Maven

I'm really confused at how your using this static variable. Is it a local variable within a function? Maybe if you gave us an example of how you intend to use it..i.e. Post the code that's supposed to use the static variable.

gerard4143 371 Nearly a Posting Maven

For a pointer to be valid you must assign it memory to point to, like below

int x = 1234;
int *iptr = &x;
/*Now iptr points to x*/

or, you must allocate it memory with malloc...like below

int *iptr = (int*)malloc(1 * sizeof(int));
/*Now iptr points to the memory allocated on the heap*/
gerard4143 371 Nearly a Posting Maven

Represented

#include <stdio.h>

int main() 
{
	int mya[3][3][4];
	return 0;
}

Which is the same(memory-wise) as a single dimension array

#include <stdio.h>

int main() 
{
	int mya[36];
	return 0;
}
gerard4143 371 Nearly a Posting Maven

Its probably because getchar() is leaving a newline character in the buffer.

After your for statement put a fgetc(stdin).

for(i = 0; i < n; ++i)
        sir[i] = getchar();

	fgetc(stdin);

Daniweb has a great sticky on numerous problems and solution when dealing with input buffers.

gerard4143 371 Nearly a Posting Maven

The first one doesn't assign or allocate any memory for the pointer to point to.

gerard4143 371 Nearly a Posting Maven

I re-wrote some of your code implementing a multi-dimensional array...Please note the comments about line's greater than 100 characters.

#include <stdio.h>

#define LINE_SIZE 100

int main (void)
{
	int line_count = 0, i = 0;
	char 	str[5][100];

	printf("Please enter 5 lines of characters, press enter to continue on to the next line");

	while (line_count < 5)
	{
		printf("->");

		while ((str[line_count][i] = getchar()) != '\n' && i < LINE_SIZE)
		{ 
			i++; 
		}

		/*you will have problems here if your line(string) is greater than 100*/

		str[line_count][i] = '\0';

		line_count++;
		i = 0;
	}

	line_count = 0;
	i = 0;

	while (line_count < 5)
	{
		fprintf(stdout, "string->%s\n", &str[line_count++][0]);
	}
	return 0;
}
gerard4143 371 Nearly a Posting Maven

Line 88

#
printf("\n---------------------------…

Is incorrect.

Also, your prompting for 5 strings but I only see one place that you store the values

char str[100];

gerard4143 371 Nearly a Posting Maven

Tnx Gerard4143,

I'll try it
BTW - I've always thought of 16 as being in INT
as in (INT 4) * (INT 4) = (INT 16)
therefore
(INT x) * (INT x) "should" = (INT y)

_Later,

TNX
Shift-Stop

Well the literal value 16 is some form of integer but x1 is defined as a float type.

gerard4143 371 Nearly a Posting Maven

Actually, even though it really should be addressed, the warning itself isn't a serious problem. It certainly could be, that's why there's a warning generated, but the value is input a couple lines later. That variable is part of the problem though.

It is declared as a pointer to char, not an array of char. As a result, you can only legally hold one (1) char in the object it points to, not a C-style string. Additionally, because it can only hold one char, the use of strlen() will produce unpredictable results due to there being no NULL terminator in the array, because there is no array.

I disagree with your first assessment.

char *stri;

never has any memory assigned or allocated that's why I homed in on it.

gerard4143 371 Nearly a Posting Maven

You could try compiling with the -E switch which informs the GCC compile to run the preprocessor only.

G++ filename -E -o resultfile

Note the file produced is large.

gerard4143 371 Nearly a Posting Maven

Try compiling like this

g++ filename.cpp -Wall -ansi -pedantic -o filename

It will point to some very suspicious areas in your code.

21: warning: null argument where non-null required (argument 2)
21: warning: null argument where non-null required (argument 2)
18: warning: ‘stri’ is used uninitialized in this function

The last one is probably the problem....Please use the code tags.

gerard4143 371 Nearly a Posting Maven

I think the easiest way is, create another string and copy the original(minus the spaces) into it and then assign the temp to the original.

gerard4143 371 Nearly a Posting Maven

Your probably missing a semi-colon in the other file.

gerard4143 371 Nearly a Posting Maven

So this 'Student struct' what is it?

gerard4143 371 Nearly a Posting Maven

I have many annoying problems with this program...or my compiler has

testit.cpp: In member function ‘void Adressbok::search()’:
testit.cpp:68: error: ‘readfile’ was not declared in this scope
testit.cpp:68: error: ‘line’ was not declared in this scope
testit.cpp:72: error: cannot convert ‘std::ofstream’ to ‘char**’ for argument ‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)’
testit.cpp:73: error: cannot convert ‘std::ofstream’ to ‘char**’ for argument ‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)’
testit.cpp:76: error: cannot convert ‘std::ofstream’ to ‘char**’ for argument ‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)’
testit.cpp:77: error: cannot convert ‘std::ofstream’ to ‘char**’ for argument ‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)’
testit.cpp:78: error: cannot convert ‘std::ofstream’ to ‘char**’ for argument ‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)’
testit.cpp:79: error: ‘sokning’ was not declared in this scope
gerard4143 371 Nearly a Posting Maven

Here's a simple way to pass an array.

#include <iostream>

void myfunc(int * x)
{
	for (int i = 0; i < 7; ++i)
		std::cout << x[i] << std::endl;
}

int main()
{
	int mya[] = {1, 2, 3, 4, 5, 6, 7};

	myfunc(mya);
	return 0;
}