C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

function pointer
Goto page Previous  1, 2, 3, ... 11, 12, 13  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language (Moderated)
View previous topic :: View next topic  
Author Message
Johannes Weiner
Guest





PostPosted: Wed Jan 25, 2006 4:13 pm    Post subject: Re: 5^2 =24,10^2=99 ???why??? Reply with quote



I rewrote this under Linux, dunno what system you are using. But i needed math.h
for pow().
Here is what i got:

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

int main(int argc, char **argv)
{
int n = 0, r = 0, r1 = 0;
char again;

do {
printf("Enter a #: ");
scanf("%d", &n);
r = pow(n, 2);
r1 = n*n;
printf("pow(%d, 2): %d, %d*%d: %d\n", n, r, n, n, r1);

printf("another run? (y/n): ");
scanf("%c", &again);
} while (again == 'Y' || again == 'y');

return 0;
}

hnaz@immortel:~$ ./foo
Enter a #: 5
pow(5, 2): 25, 5*5: 25
another run? (y/n): n

HTH, Hannes
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Johannes Weiner
Guest





PostPosted: Wed Jan 25, 2006 4:13 pm    Post subject: Re: lvalue assignment error Reply with quote



On 2006-01-10, Remco Rijnders <remco (AT) webconquest (DOT) com> wrote:
Quote:
void *blech; /* yeah yeah, I know, I know. */

char *sstrdup(const char *s)
{
AllocCalled++;
(char *) blech = strdup(s);

if (blech == NULL) {
perror("sstringdup");
abort();
}

return (char *) blech;
}

When I try to compile this code the compiler bails out throwing an "invalid
lvalue in assignment" error in my face.

This is because of the typecast.

Quote:
Removing the first (char *) is what's needed to get this section of code to
compile. It also compiled fine when I was on a 32 bit platform and with an
older (pre 4.0) GCC compiler.

foo.c:7: warning: use of cast expressions as lvalues is deprecated

You don't need to typecast the left value explicitly, it will be done
automatically.

Code:


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

char *bla(const char *s)
{
        void *foo;
        foo = strdup(s);

        if (!foo) {
                perror("bla");
                return NULL;
        }

        return (char *) foo;
}

int main(int argc, char **argv)
{
        printf("bla(): %s\n", bla("Hallo"));
        return 0;
}



This works. Without typecasting foo when assigning from strdup().
But it contains a memory leak, so do not use it like that.


HTH, Hannes
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Dave Thompson
Guest





PostPosted: Wed Jan 25, 2006 4:13 pm    Post subject: Re: incompatible warning in C Reply with quote



On 10 Jan 2006 03:37:18 GMT, "Ali Labed" <labed (AT) nortel (DOT) com> wrote:

Quote:
typedef struct s_lsp_def;

This says you have a struct tag 's_lsp_def' but does not declare any

typedef (alias) for it.

Quote:
typedef struct
{
snip
struct s_lsp_def* ptr_next_lsp_def;
}s_lsp_def;

This declares s_lsp_def as an alias for a struct type with no tag.

The struct-with-no-tag that is the target of a s_lsp_def is not the
same as, and not compatible with, a struct with tag s_lsp_def.

Either:

typedef struct s_lsp_def s_lsp_def; /* declare typedef */

struct s_lsp_def {
/* body using either s_lsp_def or struct s_lsp_def */
}; /* fill out struct */
/* note no typedef keyword or typedef-name here */

or all in one:

typedef struct s_lsp_def {
/* body using struct s_lsp_def, typedef not available */
} s_lsp_def;

or drop the typedef:

struct s_lsp_def {
/* body using struct s_lsp_def */
};
/* and use 'struct s_lsp_def'' in your code */

See FAQ 2.1 (and more of 2) at usual places and http://c-faq.com/

- David.Thompson1 at worldnet.att.net
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Dave Thompson
Guest





PostPosted: Wed Jan 25, 2006 4:13 pm    Post subject: Re: 5^2 =24,10^2=99 ???why??? Reply with quote

On 10 Jan 2006 03:37:56 GMT, "hannabaal" <hannabaal (AT) yahoo (DOT) com> wrote:

Quote:
hello all
this is my first steps in c programming.
any help for th follwing source code.
why it's giving wrong result for 5 and it multiples.
tku.

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

You also need <math.h> for pow(). Without it (on most implementations)

our program should have produced much more grossly wrong results than
you indicate in your subject line.

Quote:
int main(int argc, char *argv[])
{

int nombre;
int r=0 , r1=0;
char again;

do


{ printf("enter nombre\n");
scanf ("%d",&nombre);

You don't check whether the input (scanf) succeeded, and may proceed
to use a wrong or even garbage value if it didn't. This is probably
not a problem for you yet as long as you run only very small programs
within a few minutes of writing them.

Quote:
r= pow(nombre,2);

pow() (if correctly declared using <math.h>) does a floating-point
computation. With very few exceptions computer floating-point
computations are only approximate; in this case probably 5 up 2 was
computed as 24.999999 something and 10 up 2 as 99.99999 etc.
When stored (or otherwise converted) to an int the fractional part is
"truncated" (discarded).

Quote:
r1=nombre*nombre;
printf("Salut...\n%d au carre = %d
!\n%d*%d=%d\n",nombre,r,nombre,nombre,r1);

This line break should not be and probably was not in your source.

Quote:
printf ("do you want another operation? (Y/N)");
scanf ("%s", &again);

This is wrong. Assuming the user follows instructions, you try to
store a string of one character PLUS A NULL BYTE TERMINATOR into a
single char variable, where it doesn't fit. This causes Undefined
Behavior. If the user types more than one (adjacent nonwhitespace)
character it is even worse.

Quote:
}
while(again == 'Y' || again == 'y');


system("PAUSE");

This is nonstandard and doesn't work on some systems. Fortunately it
is also totally unnecessary here, and in most other cases it is used.

Quote:
return 0;
}

- David.Thompson1 at worldnet.att.net
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Dave Thompson
Guest





PostPosted: Wed Jan 25, 2006 4:13 pm    Post subject: Re: lvalue assignment error Reply with quote

On 10 Jan 2006 03:40:10 GMT, Remco Rijnders <remco (AT) webconquest (DOT) com>
wrote:

Quote:
void *blech; /* yeah yeah, I know, I know. */

(char *) blech = strdup(s);

When I try to compile this code the compiler bails out throwing an "invalid
lvalue in assignment" error in my face.

As it should. In standard C the result of a cast is an rvalue and

cannot be assigned to. Treating it as an lvalue is a GNU extension.

Quote:
Removing the first (char *) is what's needed to get this section of code to
compile. It also compiled fine when I was on a 32 bit platform and with an
older (pre 4.0) GCC compiler.

Then you weren't using -ansi or equivalent e.g. -std=c89 .


Quote:
I seek to understand why I get this error. While I've read to never cast
return values, I don't see why that would give an error here seeing how the
man page for strdup says a character pointer is returned.

The problem is not with the return type of strdup().


- David.Thompson1 at worldnet.att.net
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Jeffrey Schwab
Guest





PostPosted: Wed Jan 25, 2006 4:13 pm    Post subject: Re: make editor using C Reply with quote

nsh4all wrote:
Quote:
Hi all,
I want a source code for making a simple text editor using C
language. Please anyone help me for this.
Thanks in advance,
nsh.

http://ex-vi.sourceforge.net/
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Lucien Kennedy-Lamb
Guest





PostPosted: Wed Jan 25, 2006 4:13 pm    Post subject: Re: incompatible warning in C Reply with quote

Ali Labed wrote:
Quote:
incompatible warning in C

I am using MS Visual C/C++ 6.0. I get the following warning when compiling

C:model.ex.c(1221) : warning C4133: '=' : incompatible types - from
'struct s_lsp_def *' to 'struct s_lsp_def *'


In the .h file, I have
======================

typedef struct s_lsp_def;

typedef struct
{
int lsp_id;
int priority;


struct s_lsp_def* ptr_next_lsp_def;


}s_lsp_def;

in a .c file, model.ex.c, I have
================================

int add_lspToList ( s_lsp_def* the_ptr_lsp_def )
{

s_lsp_def* curr_ptr_lsp_def;

// the following line is the PROBLEM

curr_ptr_lsp_def->ptr_next_lsp_def = the_ptr_lsp_def;


}


Does anyone understand why this warning.
Thanks.

I think you're confusing the compiler namespaces. Structures (always
declared using the struct keyword prefixed) are actually in a different
namespace to typedefs. In your code you don't actaully make an
explicit declaration of what struct s_lsp_def is.

Name the structure.

Remove the first typedef struct s_lsp_def; line . This is meaningless
and shouldn't even compile!

Change the typedef declaration line to:
typedef struct s_lsp_def

Everything should work.

For clarity, I alway name the struct differently from the typedef.
Something like:

typedef struct s_lsp_def_struct
{
int lsp_id;
int priority;

struct s_lsp_def_struct *ptr_next_lsp_def;
} s_lsp_def;

Lucien Kennedy-Lamb
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Patrick Morris
Guest





PostPosted: Wed Jan 25, 2006 4:13 pm    Post subject: Re: lvalue assignment error Reply with quote

Remco Rijnders wrote:
Quote:
Hi,

I have the following snippet of code that used to compile without warnings
before:

void *blech; /* yeah yeah, I know, I know. */

char *sstrdup(const char *s)
{
AllocCalled++;
(char *) blech = strdup(s);

if (blech == NULL) {
perror("sstringdup");
abort();
}

return (char *) blech;
}


When I try to compile this code the compiler bails out throwing an "invalid
lvalue in assignment" error in my face.

Removing the first (char *) is what's needed to get this section of code to
compile. It also compiled fine when I was on a 32 bit platform and with an
older (pre 4.0) GCC compiler.

I seek to understand why I get this error. While I've read to never cast
return values, I don't see why that would give an error here seeing how the
man page for strdup says a character pointer is returned.

You appear to be casting the wrong thing. That is to say you should write:


blech = (void *) strdup(s);

I believe the error is because you have cast blech to a char * and done
nothing with it.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Ulrich Ottersbach
Guest





PostPosted: Wed Jan 25, 2006 4:13 pm    Post subject: Re: incompatible warning in C Reply with quote

"Ali Labed" <labed (AT) nortel (DOT) com> schrieb im Newsbeitrag
news:clcm-20060109-0021 (AT) plethora (DOT) net...
Quote:
incompatible warning in C

I am using MS Visual C/C++ 6.0. I get the following warning when compiling

C:model.ex.c(1221) : warning C4133: '=' : incompatible types - from
'struct s_lsp_def *' to 'struct s_lsp_def *'


In the .h file, I have
======================

typedef struct s_lsp_def;

typedef struct
{
int lsp_id;
int priority;


struct s_lsp_def* ptr_next_lsp_def;


}s_lsp_def;

in a .c file, model.ex.c, I have
================================

int add_lspToList ( s_lsp_def* the_ptr_lsp_def )
{

s_lsp_def* curr_ptr_lsp_def;

// the following line is the PROBLEM

curr_ptr_lsp_def->ptr_next_lsp_def = the_ptr_lsp_def;


}
Ali,


the problem seems to be that your code does not distinguish cleanly between
the struct and the type defined by it: inside the struct you are using the
type name as a struct tag.
Instead, try the following in your header:

struct s_lsp_def_tag
{
int lsp_id;
int priority;

struct s_lsp_def_tag *ptr_next_lsp_def;
};
typedef struct s_lsp_def_tag s_lsp_def;

It is also possible to combine the struct definition and the typedef as you
did, but that is not recommended style (I remember this from van der Linden,
Expert C Programming).

Best regards,

Ulrich
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Jeremy Brubaker
Guest





PostPosted: Wed Jan 25, 2006 4:15 pm    Post subject: Re: 5^2 =24,10^2=99 ???why??? Reply with quote

On 2006-01-10, hannabaal <hannabaal (AT) yahoo (DOT) com> wrote:
Quote:
why it's giving wrong result for 5 and it multiples.

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

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

int nombre;
int r=0 , r1=0;
char again;

do


{ printf("enter nombre\n");
scanf ("%d",&nombre);
r= pow(nombre,2);
r1=nombre*nombre;
printf("Salut...\n%d au carre = %d
!\n%d*%d=%d\n",nombre,r,nombre,nombre,r1);
printf ("do you want another operation? (Y/N)");
scanf ("%s", &again);
}
while(again == 'Y' || again == 'y');


system("PAUSE");
return 0;
}

Read the documentation for pow(). It returns a double, not
an int.

--
If the enemy is in range, so are you.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
WillerZ
Guest





PostPosted: Mon Jan 30, 2006 6:01 pm    Post subject: Re: Globals. Reply with quote

Section 6.7.8 paragraph 10. Basically you get an automatic zero or NULL
if it's a static variable and an indeterminate value otherwise.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Douglas A. Gwyn
Guest





PostPosted: Mon Jan 30, 2006 6:01 pm    Post subject: Re: Globals. Reply with quote

Jasen Betts wrote:
Quote:
does the standard aywhere state that global variables will be initialsed
with 0 if not explicitly initialised?

Yes.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Flash Gordon
Guest





PostPosted: Mon Jan 30, 2006 6:02 pm    Post subject: Re: Globals. Reply with quote

Jasen Betts wrote:
Quote:
does the standard aywhere state that global variables will be initialsed
with 0 if not explicitly initialised?

Yes. That being a 0 of an appropriate type, not necessarily all bits 0
(the null pointer and floating point 0 might not be all bits 0). Note
that at least some embedded compilers have historically ignored this
requirement.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Hans-Bernhard Broeker
Guest





PostPosted: Mon Jan 30, 2006 6:02 pm    Post subject: Re: Globals. Reply with quote

Jasen Betts <jasen (AT) free (DOT) net.nz> wrote:
Quote:
does the standard aywhere state that global variables will be initialsed
with 0 if not explicitly initialised?

Yes and no.

"No" because the standard never talks of "global variables". It calls
the ones you probably mean variables of static storage duration and
external linkage.

"Yes" because indeed, such variables are guaranteed to be initialized
before their first usage, either to the explicit initializer value
given in the source, or to an appropriate variant of "zero".

And this is done not only for "global" variables, but for all
variables of static storage duration. That includes module-local
variables (external to all functions, marked 'static' to hide them
from other modules) and local statics in functions or blocks.

--
Hans-Bernhard Broeker (broeker (AT) physik (DOT) rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Frodo Baggins
Guest





PostPosted: Mon Jan 30, 2006 6:02 pm    Post subject: [comp.lang.c.moderated]Re: make editor using C Reply with quote

Dag-Erling Smørgrav wrote:
Quote:
"nsh4all" <nsh4all (AT) gmail (DOT) com> writes:
I want a source code for making a _simple_ text editor using C
edited
language. Please anyone help me for this.

##Dag-Erling Smørgrav - des (AT) des (DOT) no wrote
ftp://ftp.gnu.org/pub/gnu/emacs/emacs-21.4a.tar.gz
Me:

emacs is *not* a simple editor
It may be simple to use, but not simple to read the code, methinks.
My 2p.

Regards,
Frodo B
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language (Moderated) All times are GMT
Goto page Previous  1, 2, 3, ... 11, 12, 13  Next
Page 2 of 13

 
 


Powered by phpBB © 2001, 2006 phpBB Group