gerard4143 371 Nearly a Posting Maven

Which operating system are we talking about here?

gerard4143 371 Nearly a Posting Maven

Not sure if this is what your looking for..

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

struct mystr
{
	char ch1[50];
	char ch2[50];
};

void myfunc(struct mystr *str1, struct mystr *str2)
{
	memcpy(&str2->ch1[0], &str1->ch1[0], sizeof(str1->ch1));	
	memcpy(&str2->ch2[0], &str1->ch2[0], sizeof(str1->ch2));		
}

int main(int argc, char**argv)
{
	struct mystr str1 = {"string one", "string two"};
	struct mystr str2;

	fprintf(stdout, "str1 ch1->%s, ch2->%s\n", str1.ch1, str1.ch2);
	fprintf(stdout, "str2 ch1->%s, ch2->%s\n", str2.ch1, str2.ch2);

	myfunc(&str1, &str2);

	fprintf(stdout, "str1 ch1->%s, ch2->%s\n", str1.ch1, str1.ch2);
	fprintf(stdout, "str2 ch1->%s, ch2->%s\n", str2.ch1, str2.ch2);	
	exit(EXIT_SUCCESS);
}
WaltP commented: If it IS what the OP is looking for, why are you GIVING it to him? You should know better. -2
gerard4143 371 Nearly a Posting Maven

Here's a simple fork/pipe example that passes data from the child to the parent.

Note you'll have to scroll down a few pages to find it:

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

This simple example has most of the elements that you'll need..

gerard4143 371 Nearly a Posting Maven

H there, actually it is a requirement for this assignment....out of curiosity, how would you implement with threads...

If your having problems with fork() then threads should probably wait. One question, how are you coordinating the calculating and passing of data between the two processes(parent and forked child)..

gerard4143 371 Nearly a Posting Maven

Unless its a requirement I won't use fork in this situation...I would use threads..

gerard4143 371 Nearly a Posting Maven

Check this posting for a pipe example:

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

gerard4143 371 Nearly a Posting Maven

g++ -Wno-trigraphs -Wno-unused -Wno-deprecated -Wpointer-arith

Why are you posting C++ in the C forum?

gerard4143 371 Nearly a Posting Maven

Isn't fgets only used from reading in from a file? Also

fgets(a, sizeof(a, stdin) );

gives error too few arguments

Also I realized maybe I should have in arrays instead of char arrays because I am working with bits (1's and 0's) so fgets shouldn't work

Too few for fgets and too many for sizeof..

fgets(a, sizeof(a), stdin );
gerard4143 371 Nearly a Posting Maven

Try piping the the response back into the program like:

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

enum PIPES {READ, WRITE};

int main(int argc, char**argv)
{
	int i = 0;
	char ans[256];
	char ch;
	int hpipe[2];
	pipe(hpipe);

	if (fork())
	{
		close(hpipe[WRITE]);
		dup2(hpipe[READ], 0);
		close(hpipe[READ]);
		while ((ch = fgetc(stdin)) != EOF)
		{
			ans[i++] = ch;
		}
		ans[i] = '\0';
		fprintf(stdout, "this is the respones stored in ans->%s\n",ans);
	}
	else
	{
		close(hpipe[READ]);
		dup2(hpipe[WRITE], 1);
		close(hpipe[WRITE]);
		execlp("uname", "uname", "-a", NULL);
	}
	exit(EXIT_SUCCESS);
}
gerard4143 371 Nearly a Posting Maven
struct my_struct *org = (struct my_struct*)malloc(sizeof(struct my_struct));

A silly question, but why do I need malloc here? I am only reading from this address.

This was before I realized what your specs were

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

#define SOMEADDRESS 0x12345678

struct my_struct{
	unsigned long *val1;
	unsigned long *val2;
};

int main(int argc, char**argv)
{

	struct my_struct *org = (struct my_struct*)SOMEADDRESS;
	org->val1 = /*assign your values here*/
	org->val2 = /*assign your values here*/
	exit(EXIT_SUCCESS);
}
gerard4143 371 Nearly a Posting Maven
Do you want the struct elements to be pointers? Or do you really mean for them to be back-to-back long integers packaged in a struct?

Not sure really but I think the struct elements must be pointers.

Thanks for your post. I'll try that as well.

ibug.

Not really, if the memory your interested in is continuous then Dave Sinkula's method is more practical..

gerard4143 371 Nearly a Posting Maven

char slide[20] and int slide(FILE *ifp, char delimeter[], char wild);
are both defined to mean two different things...I'm guessing that slide[20] is hiding the function slide.

gerard4143 371 Nearly a Posting Maven

I'm using neither. And I did mention that assigning an integer pointer to the memory location does work. So the question is not whether this is possible or not (it is possible) but what is it that I'm missing regarding the STRUCT.

Thanks.

Just curious, what are you developing this on?

For your struct:

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

struct my_struct{
	unsigned long *val1;
	unsigned long *val2;
};

int main(int argc, char**argv)
{

	struct my_struct *org = (struct my_struct*)malloc(sizeof(struct my_struct));
	org->val1 = (unsigned long*)NULL;//assign your values here
	org->val2 = (unsigned long*)NULL;//assign your values here
	exit(EXIT_SUCCESS);
}
gerard4143 371 Nearly a Posting Maven

The memory location is already allocated by the "other" program. It is not shared in that I don't write to it. It is shared in that programA writes and programB (mine) reads from this location.

But why do I need to allocate memory only when I use the STRUCT?
This line:

unsigned long *val1 = (void *) address1;

is perfectly capable of reading the shared memory location.

Unless your using some form of memory sharing then user processes can't shared data...Well I'm assuming a modern operating system with virtual memory..

gerard4143 371 Nearly a Posting Maven

Hi,
I have a program that writes to a specific memory location. I can currently read the value doing this:

#define address1 0x000F0000 //this is the memory location
unsigned long *val1 = (void *) address1;

So * val1 contains what I need from the other program.
Now I want to organize these values in a struct, something like this:

struct my_struct{
	unsigned long *val1;
	unsigned long *val2;
} pt;

struct my_struct *org;
org = &pt;

But somehow, I can't assign the value in address1 to val1.
e.g.

org->val1 = (void *) address1; //or something like that.

Any ideas what is it that I'm missing?


Many thanks,

ibug

You can't just assign memory in a user process, you have to allocate it first.

Plus this line

So * val1 contains what I need from the other program.

Are you creating a program that shares memory with another program?

gerard4143 371 Nearly a Posting Maven

You have so many things like below that I don't really know what your trying to accomplish here..

free (successor);//what's this do? Free the function??


temp = search2(&(*root),val);//what's this function search2?
printf("\n\n");
view(temp,0);
 
if(temp -> left != NULL && temp -> right != NULL)
{
	printf("pasok sa case 3");
	suc = successor(&temp);
gerard4143 371 Nearly a Posting Maven

please help me find the error in this code. whenever i try to get the successor, the program gets the status access violation error. i think it's the parameter.

You think its the parameter...How are passing it to the function and what are some of the values your passing..

gerard4143 371 Nearly a Posting Maven

Could we see what you have so far...

gerard4143 371 Nearly a Posting Maven

Should you be using SEEK_SET in this loop maybe try using SEEK_CUR

while ( ! feof( infp) ) // entering infinite dont know cause
{
fread ( &P, sizeof ( P ), 1 , infp );
cnt_rec++;
fseek( infp, sizeof(P) * ( cnt_rec - 2 ) , SEEK_SET );
fwrite ( &P, sizeof ( P ), 1, infp );
fseek( infp, sizeof(P) * cnt_rec , SEEK_SET );
}
gerard4143 371 Nearly a Posting Maven

The only thing that comes to mind is a using USB key to protect your program settings...What are you writing that requires that kind of security?

gerard4143 371 Nearly a Posting Maven

how about this kind of pyramid?
*
* * *
* * * **
* * * * * * *
* * * * * * * * *

Yeah. How about it?

Nick Evan commented: :) +12
gerard4143 371 Nearly a Posting Maven

Here's an example that will demonstrate why you can't address bits in Intel/AMD. Run the program below and note the addresses of the characters in the string "Hello, World!\n". You'll note they differ by one which is the smallest unit that can be addressed which just happens to be one character. The result of this? We have to use bit operations to mask out individual bits because the CPU doesn't understand the concept of address + 1/8 or address + 1/4.

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

char ch[] = "Hello, World!\n";

int main(int argc, char**argv)
{
	int i = 0, len = strlen(ch);

	for (i = 0; i < len; ++i)
	{
		fprintf(stdout, "ch[%d] has the value of %c and address->%p\n", i, ch[i], (void*)&ch[i]);
	}
	exit(EXIT_SUCCESS);
}
gerard4143 371 Nearly a Posting Maven

I can explain it in another way: When we open a files binary mode , we can read int, char (also bye on Java) ... When we read a integer, it reads 16 bits, when we read char, it reads 32 bit... But I want to read 1 bit.

You can't read/address 1 bit, you have to use bit masking or bit shifting to interrogate anything smaller than 1 byte...its a hardware limitation...

gerard4143 371 Nearly a Posting Maven

I think your problem stems for the fact that you can't read anything smaller than a byte on a Intel/AMD computer. To accomplish interrogations smaller than a byte, you must resort to bit masking or bit shifting...

gerard4143 371 Nearly a Posting Maven

Do you mean reading a file that's composed of the character s '0' and '1'...because when you read/write a file the data is binary...

gerard4143 371 Nearly a Posting Maven

Okay, done! Thanks gerard4143!
Now i can go back to my double-link list and DEQUE program.

There is also another clarification. How do I fix the memory leak? I've made an attempt inside the program. Please check whether it's correct or not...

void change(struct test **some_ptr)
{
    struct test *new_fresh = (struct test *)malloc(sizeof(struct test));

    free(*some_ptr);        //  SHOULD I BE DOING SOMETHING LIKE THIS?
                            //  OR IS THIS REDUNDANT?
                            //  PESONAL OPINION - IT SHOULD BE DONE

    *some_ptr = new_fresh;

    new_fresh -> link = NULL;

    new_fresh -> data = 20;

    printf("\n\n\tInside Function");
    printf("\n\n\tData = %d\t\tAddress %x", new_fresh -> data, new_fresh);
    printf("\n\n\t____________________________________________________________");
}

Yes this will get ride of the memory leak...but it will also get get of the pointer you passed to the function.
The point I'm trying to make is, is this the functionality you want in this function? Dynamic memory runtime errors are very hard to track down so I would take a minute and think about it...

gerard4143 371 Nearly a Posting Maven

I think this is what you want: - Note your code generates a memory leak on line 43...

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


struct test
{
    int data;
    struct test *link;
};

void change(struct test **ptr);

int main()
{
	struct test *fresh = (struct test *)malloc(sizeof(struct test));

	int some_data = 10;

	fresh -> data = some_data;
	fresh -> link = NULL;

	printf("\n\n\tBefore Going Into Function");

	printf("\n\n\tData = %d\t\tAddress %p", fresh -> data, (void*)fresh);
	printf("\n\n\t____________________________________________________________");

	change(&fresh);

	printf("\n\n\tAfter Getting Out Of Function");

	printf("\n\n\tData = %d\t\tAddress %p", fresh -> data, (void*)fresh);
	printf("\n\n\t____________________________________________________________");

	return 0;
}

void change(struct test **some_ptr)
{
	int some_new_data = 20;

	struct test *new_fresh = (struct test *)malloc(sizeof(struct test));

	*some_ptr = new_fresh;

	/*(*some_ptr) -> data = some_new_data;
	(*some_ptr) -> link = NULL;*/
	new_fresh->data = some_new_data;
	new_fresh->link = NULL;  

	printf("\n\n\tInside Function");
	printf("\n\n\tData = %d\t\tAddress %p", new_fresh -> data, (void*)new_fresh);
	printf("\n\n\t____________________________________________________________");
}
gerard4143 371 Nearly a Posting Maven

I've seen this extension to the language to handle binary constants...you might be able to incorporate it. Notice the number 0b111

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

int main(int argc, char**argv)
{
	fprintf(stdout, "ans->%u\n", 0b111);
	exit(EXIT_SUCCESS);
}
gerard4143 371 Nearly a Posting Maven

If your using number/base then I would check out the function strtol()

gerard4143 371 Nearly a Posting Maven

To help put this to rest, try running this code

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

int main()
{
	int i, j;
	fprintf(stdout, "----  %s  ----\n", "%d %d"+scanf("%d %d", &i, &j));
	return 0;
}

You should get output similar to this:

---- %d ----

Which is what is passed to the second scanf..

gerard4143 371 Nearly a Posting Maven

I looked at your code...well I tried to compile it and couldn't because you have too much reliance on conio.h which isn't available on my machine...You'll have to wait for a Windows programmer...

gerard4143 371 Nearly a Posting Maven

You post 376 lines of code and don't tell us what the problem is?

gerard4143 371 Nearly a Posting Maven

@Salem
This is one of those problem sets which prepares you for the job interviews.

Check out this link...This is posted as a job interview question

http://www.geekinterview.com/question_details/20527

gerard4143 371 Nearly a Posting Maven

Thank you for the nice code. It's almost solved the problem i present here.
I appreciate it. However, my original problem is more complicated.
One of them is that the user should able enter more than one digit value.

Another thing is, the number float data actually is dynamic, not just 4 float data as i present here, sometimes more than 100.
I realized, such condition will need "malloc" or "calloc".

Since the number of float data is dynamic, the user in particular condition will enter more than 4 number.

Looks a like every point i mention related to dynamic condition. Pheww..such hard problem for me.

I will try to adopt your code in my problem. Further suggestion and help are very welcome.

regards,

me

If you require a dynamic solution, then consider an integer array to hold your digit values..

int number;

int *myarray = (int*)malloc(number * sizeof(int));

myarray[0] = first value
myarray[1] = second value
myarray[2] = third value
myarray[3] = fourth value
.
.
.
myarray[number -  1] = last value
gerard4143 371 Nearly a Posting Maven

You really should be able to do this with one while loop and one for loop. The while loop reads the file and the for loop takes care of the number of entries.

I tried playing with your code and came up with this, its hard coded to a four digit code - for example when prompted the user enters a 4 digit number code, say 3267 which will print the first entry 3 times and the second entry 2 times and the third entry 6 times and finally the fourth entry 7 times...Now this piece of code is very limited in that each entry can only have the values 0 - 9, If you try a two digit value then the code breaks down...

It a nice demo on how to use the while to read the file and the for loop to handle how many times to write

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

int main() 
{
	FILE *fileread,*fileprint;
	char filestr[10];
	int tnum = 0;
	int multi = 1000;
	int i = 0;

	if (!( fileread = fopen("datafile.txt", "r")))
	{
		printf ("**** datafile could not be opened.\n");
		exit(EXIT_FAILURE);
	}

	if (!(fileprint = fopen ("copiesdata.txt", "w+")))
	{
		printf ("**** copiesdata could not be opened.\n");
		exit(EXIT_FAILURE);
	}


	fputs("enter the 4 digit number code->", stdout);
	fscanf(stdin, "%d", &tnum);
	

	while ((fscanf(fileread, "%s", &filestr[0])) != EOF)
	{
		for (i = 0; i < (tnum - (tnum % multi))/multi; ++i)
			fprintf(fileprint, "%s\n",filestr);

		tnum -= (tnum - (tnum % multi));
		multi /= 10;
	} …
me_roy commented: nice alternative +2
gerard4143 371 Nearly a Posting Maven

uh-ooh you aroused the 'C decency committee'

gerard4143 371 Nearly a Posting Maven

gcc on Debian Lenny

Well the code I gave you was compiled on GCC/Mandriva and it ran without a hitch.

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

char *ch = "this is the 34\00134\n";

int main(int argc, char**argv)
{
	int i = 0;

	for (i = 0; i < strlen(ch); ++i)
		fprintf(stdout, "ans->%x\n", ch[i]);
	exit(EXIT_SUCCESS);
}

your telling me when you ran this you got a different output then below:

ans->74
ans->68
ans->69
ans->73
ans->20
ans->69
ans->73
ans->20
ans->74
ans->68
ans->65
ans->20
ans->33
ans->34
ans->1
ans->33
ans->34
ans->a

gerard4143 371 Nearly a Posting Maven

Hi gerard4143, i tried with your source and did not work too.

Can I ask...What are you using for a compiler.

gerard4143 371 Nearly a Posting Maven

Not really sure how this is a "brain teaser in C".

gerard4143 371 Nearly a Posting Maven

Because with virtual memory every user process gets the same address range but different physical memory links...so every process appears to have the same memory but in reality it has its own copy of things like data...Note: most programs will shared some blocks of code i.e. Dlls and the text section of code can be shared..

As an experiment try changing the child processes x value and then display it....add x to the pid and then display it

ithelp commented: Thanks. +6
gerard4143 371 Nearly a Posting Maven

Try running this program. What was the result

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

char *ch = "this is the 34\00134\n";

int main(int argc, char**argv)
{
	int i = 0;

	for (i = 0; i < strlen(ch); ++i)
		fprintf(stdout, "ans->%x\n", ch[i]);
	exit(EXIT_SUCCESS);
}
gerard4143 371 Nearly a Posting Maven

I tried like you said.

Look how i did:

char *clients = "35:Jhon\x00135:Lucas";

Each client needs to start with the index 35 and I need separate them with the special char...

See? I need add the special char between the clients, and does not works because of the 35, the compiler thrown an error : "escape sequence out of range"

and now?

please... help me :)

try octal notation

char *clients = "35:Jhon\00135:Lucas";

gerard4143 371 Nearly a Posting Maven

Without even reading your code..I know this is incorrect

int salary_deduct=0;salary_deduc t> 20;salary_deduct++;
int bonus=0;bonus > 1;bonus++;

The lines salary_deduc t> 20 and bonus > 1 have no effect. Plus when you write code like this its impossible to separate declarations and code.

It should be like below:

int salary_deduct = 0;
int bonus = 0;	

salary_deduct++; 
bonus++;
gerard4143 371 Nearly a Posting Maven

Try this link

http://msdn.microsoft.com/en-us/library/h21280bw%28VS.80%29.aspx

In your example try using the octal notation.

gerard4143 371 Nearly a Posting Maven

This is kind of what I meant by posting your code with tags, notice the indentation...You have so many problems with this code its hard to know where to begin...

A hint - when you write programs compile often and when you have errors or warnings fix them before you move on...

This is a no no..Number one its syntactically incorrect and two its just hard to read.

int salary_deduct=0;salary_deduct>20;salary_deduct++;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
	char fname[50];
	char lname[20];

	printf("\t//*****************St.Jago High School******************\\ \n\n");
	printf("\t------------Courtney Athlone St.Michael Reid---------------\n\n");
	printf("\t^^^^^^^^^Computer Science IA-Pay Roll System^^^^^^^^^\n\n");

	printf("Enter the employee's First Name:\n");
	scanf("%s",&fname[50]);

	printf("Enter the employee's Last Name:\n");
	scanf("%s",&lname[20]);

	return 1;
}

int add_hours()
{
	int hours;
	int mo, tu, we, th, fr;

	printf("\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
	printf("\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");


	printf("Enter daily hours for Monday:\n");
	scanf("%d",&mo);

	printf("Enter daily hours for Tuesday:\n");
	scanf("%d",&tu);

	printf("Enter daily hours for Wednesday:\n");
	scanf("%d",&we);

	printf("Enter daily hours for Thursday:\n");
	scanf("%d",&th);

	printf("Enter daily hours for Friday:\n");
	scanf("%d",&fr);
	hours=mo+tu+we+th+fr;
	printf("\nThe weekly hours are:%d\n",hours);

	printf("\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
	printf("\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");

	return 2;
}
int salary_select()
{
	int education_tax;
	int NIS_tax;
	int NHT_tax;
	int medical_dental;

	int hours;
	int salary_deduct = 0;
	int normal_pay;
	int over_time; /*Overtime pay increases the pay rate from $20 to $25*/
	int bonus = 0; /* Adds $3000 to workers going over 70 hours*/

	normal_pay = hours * 20;
	over_time = (hours - 60) * 40 + normal_pay;
	bonus = over_time + 3000;
	salary_deduct = hours * 20 - hours * 10;

	if(hours <= 40)
		printf("\nThe employee is slacking and salary will …
gerard4143 371 Nearly a Posting Maven

Could you please use code tags to post your program...

gerard4143 371 Nearly a Posting Maven

I would investigate the functions strtok() and strtof(), strtod(), strtold().

gerard4143 371 Nearly a Posting Maven

It really depends on what and how you wrote your code...Could we see it so as to make an informed decision.

gerard4143 371 Nearly a Posting Maven

try this

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

int main ()
{
	char *ca;

	setenv("QUERY_STRING", "a=1&b=2&c=3", 1);

	ca = getenv ("QUERY_STRING"); 
	printf ("QUERY_STRING = %s\n", ca);

	return 0;
}
gerard4143 371 Nearly a Posting Maven

Actually you can slim down your code even more

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

int main ()
{
	char *ca;
	char command[100];

	command[0] = '\0';  

	strcat (command, "export QUERY_STRING=");
	strcat (command, "'a=1&b=2&c=3'");

	printf ("command = %s\n", command);  
	system (command);  

	ca = getenv ("QUERY_STRING"); 
	printf ("QUERY_STRING = %s\n", ca);

	return 0;
}