Error C2040

M

Maxx

I'm getting this error whenever i'm trying to compile this piece of C
code in Microsoft Visual C++
It's a string concatenation function from K&R exercise.I'm unable to
find the source of error it's reporting that error C2040 occurred with
the following message:
void(char *,char *) differs in levels of indirection from char*(char
*,const char *)

The code is:
#include<stdio.h>
#include<string.h>
#define ML 1000
void strcat(char *sp, char *tp)
{
/* run through the destination string until we point at the
terminating '\0' */
while('\0' != *sp)
{
++sp;
}

/* now copy until we run out of string to copy */
while('\0' != (*sp = *tp))
{
++sp;
++tp;
}
}

int main(void)
{
char s[ML]="Xecutioners";
char t[ML]="Return";
strcat(s,t);
printf("Here is the concatenated string(%s)\n",s);
return 0;
}
 
M

Maxx

In



Call it str_cat, making the corresponding change in the call in
main(), and your problem will vanish. As an exercise, see if you can
work out why this solves your problem.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within

Yeah, that seems to have got rid of that nasty problem.Thanks a lot
for that.And i couldn't figure out why this solves it.I'm quite a
novice at C, i'm still learning pointers,and i've scratched my head
for several hours as to why this nifty change vanishes the error.So
please help, i can't seem to make any headway.And also one more
request could you please recommend any site from where i could learn
recursion vividly.They seem to bug me a lot!!

thanks
 
J

jameskuyper

Maxx said:
....
Yeah, that seems to have got rid of that nasty problem.Thanks a lot
for that.And i couldn't figure out why this solves it.I'm quite a
novice at C, i'm still learning pointers,and i've scratched my head
for several hours as to why this nifty change vanishes the error.

Hint: Do you have a C textbook, or a C standard library reference? If
so, look in the index for strcat, and read the corresponding section
of the book.

If you really think about it, the simple fact that I've suggested
looking in these particular places is itself a hint as to what the
problem is.
 
N

Nick Keighley

As usual the compiler is doing its best to help you. It is saying you
have a function of void(char*,char*) when it expects char*(char*,const
char*). Ignore the const for now. It's saying it "differs in levels of
indirection", this indicates a difference in pointer-ness. And lo the
compiler is expecting a return type of char* whilst you have void. Now
look at the line indicated.

so why would the compiler expect a return type of char*. This is

by changeing the name the compiler no longer confused your function
with the standard strcpy().

It is usual not to quote sigs (the bit after the "--")
Yeah, that seems to have got rid of that nasty problem.Thanks a lot
for that.And i couldn't figure out why this solves it.I'm quite a
novice at C, i'm still learning pointers,

you don't really have a pointer problem but a function type mismatch
problem.
and i've scratched my head
for several hours as to why this nifty change vanishes the error.So
please help, i can't seem to make any headway.And also one more
request could you please recommend any site from where i could learn
recursion vividly.They seem to bug me a lot!!

Your string copy example doesn't have any recursion in it. I can't
think of any good websites on the subject. K&R has some recursion in
it. Recursion means solving a small part of the problem then applying
the same function to solve the reainder of the problem.
It's difficult tocome up with a good simple example. The one used for
teaching purposes is factorial.

int factorial (int n)
{
if (n==1 || n==0)
return 1;
else
return n * factorial (n - 1);
}

notice the internal call solves a lesser problem the original funtion.

Beware this is an *illustration* of recursion. It's not the way to
implement factorial in C.
 
K

Keith Thompson

Nick Keighley said:
Your string copy example doesn't have any recursion in it. I can't
think of any good websites on the subject. K&R has some recursion in
it. Recursion means solving a small part of the problem then applying
the same function to solve the reainder of the problem.
It's difficult tocome up with a good simple example. The one used for
teaching purposes is factorial.

int factorial (int n)
{
if (n==1 || n==0)
return 1;
else
return n * factorial (n - 1);
}

notice the internal call solves a lesser problem the original funtion.

Beware this is an *illustration* of recursion. It's not the way to
implement factorial in C.

Quicksort (not to be confused with qsort) is another good illustration
of recursion. And Fibonacci numbers are an excellent illustration of
when *not* to use recursion.
 
M

Maxx

[...]




Your string copy example doesn't have any recursion in it. I can't
think of any good websites on the subject. K&R has some recursion in
it. Recursion means solving a small part of the problem then applying
the same function to solve the reainder of the problem.
It's difficult tocome up with a good simple example. The one used for
teaching purposes is factorial.
int factorial (int n)
{
    if (n==1 || n==0)
        return 1;
    else
        return n * factorial (n - 1);
}
notice the internal call solves a lesser problem the original funtion.
Beware this is an *illustration* of recursion. It's not the way to
implement factorial in C.

Quicksort (not to be confused with qsort) is another good illustration
of recursion.  And Fibonacci numbers are an excellent illustration of
when *not* to use recursion.

--
Keith Thompson (The_Other_Keith) (e-mail address removed)  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"


Yeah i know that. But the steps are hard to grasp
 
J

James Kuyper

Nick said:
so why would the compiler expect a return type of char*. This is
because strcpy() is a standard function declared in <string.h> which
you have included.

You meant strcat(), not strcpy(), correct?
by changeing the name the compiler no longer confused your function
with the standard strcpy().

Here, too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top