gerard4143 371 Nearly a Posting Maven

First, you'll have to create a < operator for your class and then you'll have to create a function that compares two objects of that class using the < operator and then call the sort algorithm

sort(your_vector.begin(), your_vector.end(), sort_function);

Here's a simple example

#include <iostream>
#include <vector>
#include <algorithm>

class myint
{

public:

    myint(int val):itsvalue(val) {}

    bool operator <(const myint & m) const { return itsvalue < m.itsvalue; }

    int getitsvalue() const { return itsvalue; }

private:

    int itsvalue;

};

bool myint_compare(const myint & a, const myint & b)
{
    return a < b;
}

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

int main()
{
    std::vector<myint> the_ints;

    for (int i = 8; i > 0; --i)
        the_ints.push_back(i);

   for (int i = 0; i < 8; ++i)
           std::cout << the_ints[i] << std::endl;

   sort(the_ints.begin(), the_ints.end(), myint_compare);

   std::endl(std::cout);

   for (int i = 0; i < 8; ++i)
           std::cout << the_ints[i] << std::endl;

    return 0;
}
gerard4143 371 Nearly a Posting Maven

ravenous beat me to the answer.

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

int main(int argc, const char **argv)
{
	char ch[] = "12";

	int ans = atoi(ch);

	fprintf(stdout, "ans->%d\n", ans);
	return 0;
}
gerard4143 371 Nearly a Posting Maven

Try something like this

#include<stdio.h>

int main()
{
	int i = 0;
	char p[10][10];
	printf("please enter 10 words\n");

	for(i = 0; i < 10; i++)
	{		
		fgets(&p[i][0], 10, stdin);
		printf("\n");
	}

	for(i = 0; i < 10; i++)
	{
		printf("%s", &p[i][0]);
		printf("\n");
	}

	return 0;
}

A few pointers..Please use code tags and never use gets() its a dangerous function.

gerard4143 371 Nearly a Posting Maven

I was playing with this a while ago...quite a while ago so I can't guarantee that any of this code will work..

Here's the code that I used.

mywrap.c

#include <Python.h>

extern int intlimit(int);
extern int glblimit(int);
extern void* intbase(unsigned long);
extern void* glbbase(unsigned long);
extern unsigned long myfunc(void*);

PyObject *testex_myfunc(PyObject *self, PyObject *args)
{
	void* val;
	PyObject *MyObj;
	unsigned long ans;
		
	if (!PyArg_ParseTuple(args, "O", &MyObj))
	{
		return NULL;	
	}
	
	val = PyLong_AsVoidPtr(MyObj);	
	
	ans = myfunc(val);
		
	return Py_BuildValue("l", ans);
}


PyObject *testex_glbbase(PyObject *self, PyObject *args)
{
	void* ans;
	unsigned long val;
	
	if (!PyArg_ParseTuple(args, "l", &val))
	{
		return NULL;	
	}
	
	ans = PyLong_FromVoidPtr(glbbase(val));
	
	return Py_BuildValue("O", ans);
}

PyObject *testex_intbase(PyObject *self, PyObject *args)
{
	void* ans;
	unsigned long val;
	
	if (!PyArg_ParseTuple(args, "l", &val))
	{
		return NULL;	
	}
	
	ans = PyLong_FromVoidPtr(intbase(val));
	
	return Py_BuildValue("O", ans);
}


PyObject *testex_intlimit(PyObject *self, PyObject *args)
{
	int ans, val;
	
	if (!PyArg_ParseTuple(args, "i", &val))
	{
		return NULL;	
	}
	
	ans = intlimit(val);
	
	return Py_BuildValue("i", ans);
}

PyObject *testex_glblimit(PyObject *self, PyObject *args)
{
	int ans, val;
	
	if (!PyArg_ParseTuple(args, "i", &val))
	{
		return NULL;	
	}
	
	ans = glblimit(val);
	
	return Py_BuildValue("i", ans);
}


static PyMethodDef testexmethods[] = 
{
	{"intlimit", testex_intlimit, METH_VARARGS,"return interrupt table limit"},
	{"glblimit", testex_glblimit, METH_VARARGS,"return global table limit"},
	{"intbase", testex_intbase, METH_VARARGS,"return interrupt base address"},
	{"glbbase", testex_glbbase, METH_VARARGS,"return global base address"},
	{"myfunc",testex_myfunc,METH_VARARGS,"get pointer value"},
	{NULL,NULL},
};

void inittestex(void)//the name of the module is testex but this function must be called inittestex 
{
	Py_InitModule("testex", testexmethods);
}

test.c

int intlimit(int val)
{
	char ch[(sizeof(unsigned short) + sizeof(void*))];
	
	__asm__ __volatile__
	( …
gerard4143 371 Nearly a Posting Maven

No your data is safe. When you reassign pointer, its just gets the address of your string "weeeeeeeeeeeeeeeeeeeeeee".

"hi" - has one address value.
"weeeeeeeeeeeeeeeeeeeeeee" - is another address value.

gerard4143 371 Nearly a Posting Maven

Wow, you really couldn't google this?

gerard4143 371 Nearly a Posting Maven

Try removing the () in

if ((textBox1->Text == ("")) &&

so that its is

if ((textBox1->Text == "") &&

Actually on further inspection, you have on line 3

(radioButton3->Checked = false)

it should be

(radioButton3->Checked == false)

gerard4143 371 Nearly a Posting Maven

Try looking at the attached code...It may help straighten things out or it just might confuse you more..

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

int main()
{
	int i = 0;
	int *iptr = (int*)malloc(sizeof(int) * 10);
	int **tptr;

	for (i = 0; i < 10; ++i)
		*(iptr + i) = i;

	for (i = 0; i < 10; ++i)
		fprintf(stdout, "ans->%d\n", *(iptr + i));

	tptr = &iptr;

	for (i = 0; i < 10; ++i)
		fprintf(stdout, "ans->%d\n", *(*tptr + i));
	
	return 0;
}

I really should have used C++, so here look at this one

#include <iostream>

int main()
{
	int i = 0;
	int *iptr = new int[10];
	int **tptr;

	for (i = 0; i < 10; ++i)
		*(iptr + i) = i;

	for (i = 0; i < 10; ++i)
		std::cout << "ans->" << *(iptr + i) << std::endl;

	tptr = &iptr;

	for (i = 0; i < 10; ++i)
		std::cout << "ans->" << *(*tptr + i) << std::endl;
	
	delete [] iptr;
	
	return 0;
}
gerard4143 371 Nearly a Posting Maven

The memory manager is choking on your pointer 'p' because you incremented its value.

Check the enclosed code..

#include<iostream>
using namespace std;

int main()
{
  int* p=new int[2];
  p[0]=1;
  p[1]=2;
  cout<<p[0]<<" "<<&p[0]<<endl;
  cout<<p[1]<<" "<<&p[1]<<endl;
  cout<<endl;
  cout<<*p<<" "<<p<<endl;
  p++;
  cout<<*p<<" "<<p<<endl;
  p--;//set pointer back to its original value
  delete [] p;
}
gerard4143 371 Nearly a Posting Maven

Line 19...Why are you closing write here?

actually line 16...Why are you opening write here?

Try looking at the code below

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

#define BSIZE 24

int main()
{
	char ch[BSIZE];
	FILE *fout;
	FILE *fin;

	if (!(fin = fopen("testfile", "r")))
	{
		fputs("could not open testfile!\n", stderr);
		exit(EXIT_FAILURE);
	}

	if (!(fout = fopen("testout", "w")))
	{
		fputs("could not open testout!\n", stderr);
		exit(EXIT_FAILURE);
	}

	while (fwrite(ch, sizeof(char), fread(ch, sizeof(char), BSIZE, fin), fout))
	{
	}

	fclose(fout);
	fclose(fin);
	return 0;
}
gerard4143 371 Nearly a Posting Maven

@kamatari..not yet..!!!
bt i am thinking of making a game and it will surely take time.
our teachers has informed us about dis project at the last moment.

If the teachers dumped this project on the class 'at the last moment' then I expect the class and yourself will have to 'do the best you can'.

jonsca commented: Well put. +6
gerard4143 371 Nearly a Posting Maven

Try changing line 29 to

myQ.AddMSG(myNewMessage);

gerard4143 371 Nearly a Posting Maven

Try this

g++ testit.cpp -E testit >testfile

gerard4143 371 Nearly a Posting Maven

Why would you want to translate something that's hard wired to a specific address?

typedef void (*t_ChatPrint) ( char * );
t_ChatPrint ChatPrint = (t_ChatPrint)0x0054E410;

Your function pointer ChatPrint is hard wired to 0x0054E410..

Can I ask you a question? Why are you translating code when you don't even know its function.

gerard4143 371 Nearly a Posting Maven

Yes that's correct if your input buffer is empty.

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

This might be your problem...

On line 14 your freeing Curr and then on line 16 you set Prev to Curr...Prev now equals NULL.

gerard4143 371 Nearly a Posting Maven
moroccanplaya commented: thanks +1
gerard4143 371 Nearly a Posting Maven

i want to put name for the query like this for e.g

chr* names;

names=mysql_query(con,"the query");

so names now = to the query result thats what i want to do!!! did u get me now?

Well if you looked at the code I posted, you would have noted that I used a pointer to MYSQL_RES to hold the result for the connection.

Please read this link, it will explain what mysql_query returns..

http://dev.mysql.com/doc/refman/5.0/en/mysql-query.html

gerard4143 371 Nearly a Posting Maven

Wow..You really should download a tutorial on mysql. You haven't established a valid connection to your database, yet your preforming a query on it...Here's a brief example

#include <mysql/mysql.h>
#include <stdio.h>

int main(int argc, char **argv) 
{
MYSQL *conn;/*connection*/
MYSQL_RES *res;/*result*/
MYSQL_ROW row;/*row*/

char server[] = "localhost";
char user[] = "root";
char password[] = ""; 
char database[] = "testdb";

conn = mysql_init(NULL);/*initialize*/

/* Connect to database */
if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) 
{
	printf("an error occurred->%s\n", mysql_error(conn));
	return 1;
}
/*perform query*/
if (mysql_query(conn, "select * from personal"))
{
	printf ("the query result->%s\n", mysql_error(conn));
	return 1;
}

/*get query result*/
res = mysql_use_result(conn);

/*print result*/
while ((row = mysql_fetch_row(res)) != NULL)
	printf("%s, %s, %s, %s, %s, %s \n", row[0], row[1], row[2], row[3], row[4], row[5]);

/* close connection */
mysql_free_result(res);
mysql_close(conn);
return 0;
}
jonsca commented: Nice example. +5
gerard4143 371 Nearly a Posting Maven

Could be you freed a pointer *and did not set it to NULL* and then tried to free it again..The message indicates the problem lies at address 0x089b8008

*** glibc detected *** ./tryp: double free or corruption (top): 0x089b8008 ***

moroccanplaya commented: thanks +1
gerard4143 371 Nearly a Posting Maven

Nope, no drawbacks. The this pointer is just a pointer to the data members of the object..If omitted in member functions its assumed.

Should you use it? It really depends on your preference.

gerard4143 371 Nearly a Posting Maven

So buff is 1024 characters long?

char buff[1024];

gerard4143 371 Nearly a Posting Maven

Because a returning function will invalidate all its variables, you can't return the address to any of them and expect consistent results...Here's one way to solve your problem using the heap.

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

#define ARR_SIZE 4

int* foo(void);

int main()
{
	int i = 0;
	int *ans = foo();

	for (; i < ARR_SIZE; ++i)
		fprintf(stdout, "ans->%d\n", ans[i]);

	free(ans);
	return 0;
}

int* foo(void)
{
	int i = 0;
	int *ar = (int*)malloc(ARR_SIZE * sizeof(int));
	
	for (; i < ARR_SIZE; ++i)
		ar[i] = i + 10;

	return ar;
}
gerard4143 371 Nearly a Posting Maven

Hi Gerard4143,
Thanks for your reply. I'm trying to create a general function that will collect the information from other processes (I have multi-processes; Yeah MPI) this function receive the information form other processes and make an operation (like here SUM) and send back the result to other process and print it.

I check the sending and receiving part and they are working properly but when I want to return the value, I have problems.

I guess we can deference a void pinters;for example the following MPI command is similar to what I wanna do;

int MPI_Reduce ( void *sendbuf, void *recvbuf, int count,
MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm )

*sendbuf is input. And *recvbuf is output (void pointer) it return the address of the variable ( that can be int, float, double, etc)

Basically I am doing something similar. Thanks again for your help.

I meant what are you trying to do in the posted code...Its my impression that your trying to pass the addresses of two arrays and then manipulate them inside said function....Note I cast the void pointers to double pointers.
Like so

testit.c

#include <stdio.h>
#include <math.h>

#define MAX 512

void chamaleon (void *sbuf, void *rbuf, int count);

int main()
{
	double sbuf[MAX];
	double rbuf[MAX];
	
	int i;

	for (i = 0; i < MAX; i++)
		sbuf[i] = (i + 1) * 1.0;

	chamaleon (sbuf, rbuf, MAX);

	for (i = 0; i < MAX; ++i)
		fprintf(stdout, "rbuf[%i]->%f\n", i, rbuf[i]); …
gerard4143 371 Nearly a Posting Maven

Its an error because you can't dereference a void pointer..rbuf is a void pointer.

*rbuf= &svar;

What exactly are you trying to a accomplish here? What is the purpose of the program?

gerard4143 371 Nearly a Posting Maven

std::cerr is an I/O channel that can be used for errors...Its almost the same as std::cout except its not buffered.

Why two asterisks? Its a pointer to a pointer...so two asterisks.

What's this line?

farray = new float*[rows * sizeof(float*)];

That is how you allocate the memory for three float pointers.

This one?

farray = new float[cols * sizeof(float)];

Here I'm allocating the arrays of floats for each of the three pointers.

and I do use ifstream.

gerard4143 371 Nearly a Posting Maven

I tried your problem but used pointers to pointers for the array...

testfile

3 6
3.9 4.5 2.1 1.0 2.4 4.3
3.1 4.2 5.1 6.2 1.0 2.7
1.2 2.3 3.1 4.2 5.2 6.4

testit.cpp

#include <iostream>
#include <fstream>

int main()
{
	int rows = 0;
	int cols = 0;
	int count = 0;
	int row_count = 0;

	float **farray = (float**)NULL;

	std::ifstream		fin("testfile");

	if (fin.fail())
	{
		std::cerr << "Could not open file" << std::endl;
		return 1;
	}

	fin >> rows >> cols;

	farray = new float*[rows * sizeof(float*)];

	for (int i = 0; i < rows; ++i)
		farray[i] = new float[cols * sizeof(float)];

	float value = 0.0;

	while (fin >> value)	
	{
		farray[row_count][count++] = value;
		if (count >= cols)
		{
			count = 0;
			++row_count;	
		}		
	}

	fin.close();

	for (int i = 0; i < rows; ++i)
	{
		for (int j = 0; j < cols; ++j)
		{
			std::cout << farray[i][j] << " ";
		}
		std::endl(std::cout);
	}
	return 0;
}

The code is pretty rough but you can clean it up and see if it works for you..

gerard4143 371 Nearly a Posting Maven

Shouldn't this

3 5
3.9 4.5 2.1 1.0 2.4 4.3
3.1 4.2 5.1 6.2 1.0 2.7
1.2 2.3 3.1 4.2 5.2 6.4

be

3 6
3.9 4.5 2.1 1.0 2.4 4.3
3.1 4.2 5.1 6.2 1.0 2.7
1.2 2.3 3.1 4.2 5.2 6.4

gerard4143 371 Nearly a Posting Maven

You have major problems in your code...I tried compiling it and set off many errors and warnings...Try starting with this shell.

#include <iostream>
#include <cstring>

class string
{

public:

	string():s(NULL), itssize(0) {}
	string(const char* const cstring);

	void displayit() const { std::cout << s << std::endl; }
private:

	char *s;
	int itssize;

};

string::string(const char* const cstring)
:itssize(strlen(cstring))
{
	s = new char[itssize + 1];
	for (int i = 0; i < itssize; ++i)
		s[i] = cstring[i];
	s[itssize] = '\0';
}

int main()
{
	string me("my string");

	me.displayit();
	return 0;
}
jonsca commented: WTG for looking into it more deeply +5
gerard4143 371 Nearly a Posting Maven

Try this one

MyMatrix‬‬: MyMatrix.o MyStringMain
	gcc MyMatrix.o -o MyMatrix

MyMatrix.o: MyMatrix.c
	gcc -c MyMatrix.c

MyStringMain: MyString.o MyStringMain.o
	gcc  MyString.o MyStringMain.o -o MyString -o MyStringRun

MyStringMain.o: MyStringMain.c MyString.c MyString.h
	gcc -c MyStringMain.c 

MyString.o: MyString.c MyString.h
	gcc -c MyString.c


.PHONY:clean
clean: 
	rm -f *.o test.out
gerard4143 371 Nearly a Posting Maven

See if you can find what I changed...

#include <stdio.h>

int NumericalAnswer;
int score = 0;

int main()
{
	    printf("Question 1: What's 4 x 3?\n");
	    scanf("%d", &NumericalAnswer);
    
	if (NumericalAnswer == 12)
	{
		printf("You got it right!");
		score += 1;
	}
	return 0;
}
NichtSoGut commented: Couldn't really have been more helpful!! +0
gerard4143 371 Nearly a Posting Maven

By size? Do you mean height or weight?

gerard4143 371 Nearly a Posting Maven

A few things...First what doesn't work? Next you should check to see if fopen was successful.

dblPoint = fopen(BINDATA, "wb");

Did fopen succeed or fail here?

gerard4143 371 Nearly a Posting Maven

Because its aligned 2 bytes its guaranteed that the address & 0x1 will return 0...Check out the attached code..

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

int main(int argc, char**argv)
{
	int i = 0;
	char __attribute__((aligned(2))) ch1;
	char *cptr = &ch1;

	for (i = 0; i < 20; ++i)	
	{
		fprintf(stdout, "ans->%u\n", ((unsigned int)(unsigned long)cptr & 0x1));
		cptr += 2;
	}


	exit(EXIT_SUCCESS);
}

The output is ans->0 for all iterations...Why do they have a function that tests for this? I really don't know.

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

Try creating a small exe with the gcc compiler then use the binutil

readelf -a exename

It should display all the information you need..

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

Ancient D. is correct. Try running this program, noticing the output when you type path and filename.

#include <iostream>
#include <string>

int main()
{
	std::string mystring;

	std::cout << "Enter path and filename->";
	std::cin >> mystring;

	for (int i = 0; i < mystring.size(); ++i)
		std::cout << mystring[i] << " ";

	std::cout << std::endl;
	return 0;
}
Jsplinter commented: thank you for the example! +1
gerard4143 371 Nearly a Posting Maven

Well I would blah, blah blah.

Please post code that is remotely correct or at least post code where the error is obvious or post an explanation of the problem...

Posting something, something, blah, blah, blah makes us wonder - are you a spam bot?

gerard4143 371 Nearly a Posting Maven

Not really sure what the objective is here? If you want to allocate memory via a function try the following code:

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

bool vector(char **theArray, int size);

int main()
{
	int i = 0;
	char *ch = (char*)NULL;

	if (!vector(&ch, 100))/*call vector and test for failure*/
	{
		fputs("Allocation failed...exiting program\n", stderr);
		exit(EXIT_FAILURE);
	}

	for (i = 0; i < 26; ++i)/*load some values*/
		*(ch + i) = ('a' + i);

	*(ch + i + 1) = '\0';/*delimit our cstring with '\0'*/

	fprintf(stdout, "ch->%s\n", ch);/*display our cstring*/

	free(ch);

	ch = (char*)NULL;

	return 0;
}

bool vector(char **theArray, int size)
{
	*theArray = (char*)malloc(size * sizeof(char));
		
	if (!theArray)
		return false;

	return true;

}
gerard4143 371 Nearly a Posting Maven

I would use the class constructor to initialize your vector to the appropriate number of elements.

Jsplinter commented: thank you +1
gerard4143 371 Nearly a Posting Maven

The general rule is, for every time you malloc you have to call free..

Why set a pointer to NULL? Because if you call free on a NULL set pointer nothing happens, also its easy to check if a pointer is NULL by examining its value..

int *ptr = (int*)NULL;

if (!ptr)/*check if pointer is NULL*/
{
/*do something*/
}

Pointers, like everything else in programming should be left in a known/safe state.

gerard4143 371 Nearly a Posting Maven

Take this out of your header file

long addresses[5];

and put it in your main executable...

When you compile def.c you create a label called addresses and when you compile mainfile.c you create another label addresses. When you bring both files together for the final executable it fails because addresses is defined in both def.o and mainFile.o.

gerard4143 371 Nearly a Posting Maven

Try

ReadFile.read((char*)p_ReadBuff, 1);
gerard4143 371 Nearly a Posting Maven

You could use

p1->lastName
p1->firstname

gerard4143 371 Nearly a Posting Maven
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

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

You have a memory leak in your = operator. You should first delete your member pointer before you create the new one.

Plus you have numerous errors in your example like:

SomeClass(int size) {myMember = new OtherClass(thingSize);};

you have size but use thingSize?

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 including stdlib.h

Also, you may want to put this at the end of the main function - return 0;