gerard4143 371 Nearly a Posting Maven

I have a question about functions and pointers to functions....

Why does a C program treat a function call to a function and a function call to a function pointer the same?

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

typedef void (*funcptr)(void);

void myfunc(void)
{
	fputs("Hello, World!\n", stdout);
}

int main(int argc, char**argv)
{
	funcptr MyFunc = &myfunc;

	myfunc();
	MyFunc();

	exit(EXIT_SUCCESS);
}

Program output:

Hello, World!
Hello, World!

When I compile the above program both the call to myfunc() and MyFunc() will produced the same result..."Hello, World" will be displayed. Now when I examine the asm instructions for this program, I find that the compiler was nice enough to dereference the function pointer for me.....Why does the language allow for this? Is there a reason to treat two different entities the same in source code but differently in the resulting asm code?....I hope this makes sense...I await your replies....Gerard4143

gerard4143 371 Nearly a Posting Maven
mystruct* A;

You define A as a pointer to mystruct but never assign or allocate memory for it.

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

thnx guys,so i need to use the "for" for(i=0;i<n;i++){scanf("%c",tab)},and in the case of using gets() the memory reserved by malloc can be changed?

I would check out fgets() like Narue said.

char *fgets(char *s, int size, FILE *stream);

gerard4143 371 Nearly a Posting Maven

Here's a blurb from manpages malloc which discusses buffer overflows(its in the second half).

NOTES
Normally, malloc() allocates memory from the heap, and adjusts the size
of the heap as required, using sbrk(2). When allocating blocks of mem‐
ory larger than MMAP_THRESHOLD bytes, the glibc malloc() implementation
allocates the memory as a private anonymous mapping using mmap(2).
MMAP_THRESHOLD is 128 kB by default, but is adjustable using mal‐
lopt(3). Allocations performed using mmap(2) are unaffected by the
RLIMIT_DATA resource limit (see getrlimit(2)).

The Unix98 standard requires malloc(), calloc(), and realloc() to set
errno to ENOMEM upon failure. Glibc assumes that this is done (and the
glibc versions of these routines do this); if you use a private malloc
implementation that does not set errno, then certain library routines
may fail without having a reason in errno.

Crashes in malloc(), calloc(), realloc(), or free() are almost always
related to heap corruption, such as overflowing an allocated chunk or
freeing the same pointer twice.

Recent versions of Linux libc (later than 5.4.23) and glibc (2.x)
include a malloc() implementation which is tunable via environment
variables. When MALLOC_CHECK_ is set, a special (less efficient)
implementation is used which is designed to be tolerant against simple
errors, such as double calls of free() with the same argument, or over‐
runs of a single byte (off-by-one bugs). Not all such errors can be
protected against, however, …

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

this cant possibly be that hard to answer?

Like you said "this cant possibly be that hard to answer". I just google C >> and checked wiki C/Operators and there they were...


http://en.wikipedia.org/wiki/C_%28programming_language%29

camdaddy09 commented: didnt help at all +0
gerard4143 371 Nearly a Posting Maven

Right away, these lines should occur outside of the main function.

int  *make_pointer(int);
void read_array(int *array[3]);

Also, you should call free the same number of times you call malloc.

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

What happened? Did an eggplant blow up in here?

jephthah commented: perfect +0
Nick Evan commented: :) +0
gerard4143 371 Nearly a Posting Maven

I know Gnome/System Monitor provides those details.

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.

gerard4143 371 Nearly a Posting Maven

Here's a link to XDR - library routines for external data representation

http://linux.about.com/library/cmd/blcmdl3_xdr.htm

gerard4143 371 Nearly a Posting Maven

You define your function header like

void product (float x, int y, float *prd)

and

void product (float, int, float)

Which are different.

gerard4143 371 Nearly a Posting Maven

instead of printing it, how can I assign it to variables or a char array? So if I enter 255.255.255.0 for a netmask, it will assign

ipray[0] = 255
ipray[1] = 255
ipray[2] = 255
ipray[3] = 0

I could then use atoi() to convert them to int and I already wrote another function that converts decimal to binary. So I should hopefully be good after I get this figured out, unless I run into problems when doing the actual math.

Could you also explain line 5 and line 29?

Thanks,

If the values are never bigger than 255 then you could use
unsigned chars for the array type which have a value range 0 - 255.
To convert then I would use atoi.

gerard4143 371 Nearly a Posting Maven

Your problem is this line right here

ipray[i] = *tok;

Which doesn't do what you think it does.

Take a look at this version which is a simplified version of your code and is correct...Now all you have to do is convert the values to integers or copy them to the character array.

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

void truncat(char str[], char ipray[]);

int main()
{
	char str[16];
	char ipray[4];

	printf("Enter a Netmask :");
	scanf("%s", &str[0]);

	truncat(str, ipray);

	return 0;
}

void truncat(char str[], char ipray[])
{
	char *tok;
	int i = 0;

	tok = strtok(str,".");

	while (tok != NULL)
	{
		fprintf(stdout, "tok->%s\n", tok);
		tok = strtok(NULL, ".");
	}
}
gerard4143 371 Nearly a Posting Maven

You could help us out and post the code.

gerard4143 371 Nearly a Posting Maven

Not really sure what your question is...

gerard4143 371 Nearly a Posting Maven

I would try printing the value of file1, just to see what the function is returning.

gerard4143 371 Nearly a Posting Maven

Are you saying the TCP software is combining several writes into a single packet?...This is usual and up to the TCP software algorithms to decide on what is most efficient.

Here's a link about forcing TCP to write data..

http://www.unixguide.net/network/socketfaq/2.11.shtml

gerard4143 371 Nearly a Posting Maven

Your structure data member aStudent

StudentType *aStudent;

Did you allocate/assign memory for the pointer?

gerard4143 371 Nearly a Posting Maven

i got a bit confused the problem i figured out i have is that program crushes when i try to use strcpy with a struct which is inside another struct.Can you help me with that?

Are you reading what I posted...strcpy copies strings not integers...Your tying to copy integers with strcpy.

strcpy(a.aStudent->finalMark,bathmos);

a.aStudent->finalMark is an integer
bathmos is an integer

just do this

a.aStudent->finalMark = bathmos;

gerard4143 371 Nearly a Posting Maven

Also you have more than one problem, here's the output when I tried to compile your program...I called it testit.c

testit.c:24:1: warning: return type defaults to ‘int’
testit.c: In function ‘main’:
testit.c:29:3: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[20]’
testit.c:30:3: warning: implicit declaration of function ‘strcpy’
testit.c:30:3: warning: incompatible implicit declaration of built-in function ‘strcpy’
testit.c:33:3: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
testit.c:34:3: warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast
testit.c:34:3: note: expected ‘char *’ but argument is of type ‘int’
testit.c:34:3: warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast
testit.c:34:3: note: expected ‘const char *’ but argument is of type ‘int’
testit.c:37:1: warning: control reaches end of non-void function
testit.c:33:8: warning: ‘bathmos’ is used uninitialized in this function

gerard4143 371 Nearly a Posting Maven

This is what my help files say about strcpy

STRCPY(3) Linux Programmer's Manual STRCPY(3)

NAME
strcpy, strncpy - copy a string

SYNOPSIS
#include <string.h>

char *strcpy(char *dest, const char *src);

char *strncpy(char *dest, const char *src, size_t n);

DESCRIPTION
The strcpy() function copies the string pointed to by src, including
the terminating null byte ('\0'), to the buffer pointed to by dest.
The strings may not overlap, and the destination string dest must be
large enough to receive the copy.

strcpy copies strings not integers.

gerard4143 371 Nearly a Posting Maven

Right away you should include the string.h library for strcpy().

This line

strcpy(a.aStudent->finalMark,bathmos);

Both variables are integers, strcpy requires char pointers.

Your first scanf

scanf("%s",&onoma);

should be

scanf("%s", onoma);
gerard4143 371 Nearly a Posting Maven

... so it must oblige to the pointer alignment requirements. If the pointer is fetched correctly, the call would succeed. However, if it is stored at the odd boundary, chances are you'd fetch not what you expect.

This UB stems from the architectural issues indeed. On most IA32 based platforms you wouldn't notice anything strange. Still it is an UB and must be avoided.

Thanks for clearing that up nezachem...

If memory serves, on a IA32 box non-aligned calls are inefficient but doable but like your posting advised, should be avoided....Thanks everyone for the input...

gerard4143 371 Nearly a Posting Maven

Regarding the original post, though well-formed, the code may invoke an undefined behaviour. The alignment of unsigned int (and a function pointer) is stronger than that of a char; a situation specifically mentioned in 6.3.2.3.7 paragraph of a Standard.

Maybe I'm missing the boat on this one...Why would it matter where I store the function pointer. The value is just a pointer and when called it should call the appropriate code or is the possible 'undefined behaviour' an architectural issue.

gerard4143 371 Nearly a Posting Maven

Thanks for the replies...I downloaded the C standard and I'm reading the section 6.3.2.3.7 now..Interesting read.

gerard4143 371 Nearly a Posting Maven

I am excited, nervous, but excited. I really want to actually do something and really get my hands dirty.

Well good luck and I hope you need a lot of soap this summer.

gerard4143 371 Nearly a Posting Maven

I have a question, well more of a verification.

In the program below, I'm packing a character array with some values. The first two are unsigned integers the next is a function pointer and the final is a string of characters...Now my question, is the casting correct for the function pointer? Just check the sections marked off with /*this section*/.

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

#define MAXLINE 4096

char *ca = "this is the string to pass";

void myhello(void)
{
	fputs("Hello, World!\n", stdout);
}

int main(int argc, char**argv)
{
	unsigned int i = 0;
	char recvline[MAXLINE];

	*(unsigned int*)&recvline[0] = 11111;
	*(unsigned int*)&recvline[sizeof(unsigned int)] = 22222;
 
	/*this section*/
	*(void(**)(void))&recvline[2 * sizeof(unsigned int)] = (void(*)(void))&myhello;
	/*this section*/

	for (i = 0; i < strlen(ca); ++i)
	{
		recvline[2 * sizeof(unsigned int) + sizeof(void*) + i] = ca[i];
	}

	fputs("\n\ndisplay packed character array\n\n", stdout);

	fprintf(stdout, "number one->%u\n", *(unsigned int*)&recvline[0]);
	fprintf(stdout, "number two->%u\n", *(unsigned int*)&recvline[sizeof(unsigned int)]);

	fputs("call function myhello->", stdout);
	/*this section*/
	(*(void(**)(void))&recvline[2 * sizeof(unsigned int)])();
	/*this section*/
	
	fprintf(stdout, "string->%s\n", &recvline[2 * sizeof(unsigned int) + sizeof(void*)]);
	return 0;
}
gerard4143 371 Nearly a Posting Maven

If the cross-complier is already setup then it shouldn't really be a problem...Program as usual but run the programs on your embedded system...If your looking for tuts and docs try googling GCC embedded systems

http://www.linuxjournal.com/article/9904

gerard4143 371 Nearly a Posting Maven

Just put a line counter in your while loop.

gerard4143 371 Nearly a Posting Maven

Try this link:

http://www.networksorcery.com/enp/protocol/ip.htm

Actually try downloading WireShark and checking out some headers.

gerard4143 371 Nearly a Posting Maven

The second loop allows us to check each element against all the other elements.

Just a note:

printf("Enter Value for %d",i);

The printf should be

printf("Enter Value for element [%d]->", i);
gerard4143 371 Nearly a Posting Maven

Look very closely

else if(xcoord<0 && ycoord>0);

Notice the semi-colon.

gerard4143 371 Nearly a Posting Maven

Why do you have these lines if your using command line agruements
to pass the values?

printf("Enter the name of the file you want to back up.>");
    scanf("%s", argv[0]);
    printf("Enter the name of the file to back up to.>");
    scanf("%s", argv[1]);
gerard4143 371 Nearly a Posting Maven

Maybe you could help me with the following code...

int main()
{
    char foo = '?';    // foo is created on the stack.
    char* bar = "Hello World";   // where is bar created?

    // will the following result as a NOP or undefined behavior?
    free(bar);

    return 0;
}

The pointer char* bar is created on the stack...."Hello World" is a literal so its probably created in the read only data section...

As for the behavior...Try it and see what happens.

gerard4143 371 Nearly a Posting Maven

Protect from who?

If you edit the files, you may end up with a copy in the swap file.

If you delete the file, the data will remain on disk until the space is re-used by the file system.

If you compile the file, then the pre-processor expanded copy of the source might be in the temp directory. Even if it's deleted, problem 2) remains.

This is a very good point or points. Maintaining a data free environment is expensive in time and procedures.

gerard4143 371 Nearly a Posting Maven

Probably the simplest and most secure is to keep all your source code on a removable media.

gerard4143 371 Nearly a Posting Maven

Where to start...At the beginning

#include <stdio.h>
#include <time.h> 

int main(int argc, char**argv)
{
	return 0;
}

Plus check out the internet...like here:

http://www.gnu.org/s/libc/manual/html_node/Time-Functions-Example.html

gerard4143 371 Nearly a Posting Maven

If you new to Linux shell programming try here:

http://tille.garrels.be/training/bash/

gerard4143 371 Nearly a Posting Maven

Don't request everything in one scanf call. Call scanf for your sentinel value the check the result and then call scanf for the remainder.

gerard4143 371 Nearly a Posting Maven

The best/practical socket programming book IMHO

Internetworking With TCP/IP Volume III: Client-Server Programming and Applications, Linux/POSIX Socket Version (with D. Stevens), 2000. 0-13-032071-4

http://www.cs.purdue.edu/homes/dec/netbooks.html

Note the book is a bit pricey.

gerard4143 371 Nearly a Posting Maven

Are you talking about the angle between the minute hand and the hour hand, measured at intervals.

gerard4143 371 Nearly a Posting Maven

Here's a working copy of part of your program...You really should write your programs in small sections. That way you can get one section working and then move onto another..Note I went with char pointer instead of char array for letter.

Please also note the formating of the program...It much easier to read.
Things like a for statement for(i=0;i<N;i++) with no spaces should be avoided.

#include <stdio.h>
#include <math.h>
#define N 2
#define ISIMUZUN 15

struct student
{
	char name[ISIMUZUN];
	char surname[ISIMUZUN];
	int number;
	int final;
	int midterm;
	int hw1;
	int hw2;
	int grade;
	char *letter;
};

int main(int argc, char *argv[])
{
	struct student info[N];
	int i = 0;

	for(i = 0; i < N; i++)
	{
		printf("Please enter student's information with blanks between them\n");
		scanf( "%d %s %s %d %d %d %d", &info[i].number, \
		info[i].name, info[i].surname, &info[i].midterm, \
		&info[i].final, &info[i].hw1, &info[i].hw2);
		info[i].letter = "AA";
	}


	for(i = 0; i < N; i++)
	{
		printf ( "%d %s %s %d %d %d %d %s\n", info[i].number, \
		info[i].name, info[i].surname, info[i].midterm, \
		info[i].final, info[i].hw1, info[i].hw2, info[i].letter);
	}
	return 0;
}
gerard4143 371 Nearly a Posting Maven

Your trying to assign a const string pointer to a char [3] with this line which is a no no.

info[i].letter= "DD";

Try something like

info[i].letter[0] = 'A';
info[i].letter[1] = 'A';
gerard4143 371 Nearly a Posting Maven

Your scanf should be reading the data into the address of the structure members..

&info.number

jephthah commented: good job. you are all over this. +7