total newb - "passing arg 2 of `strcpy' makes pointer from integer without a cast"

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2005
Posts: 8
Reputation: atrixnet is an unknown quantity at this point 
Solved Threads: 0
atrixnet's Avatar
atrixnet atrixnet is offline Offline
Newbie Poster

total newb - "passing arg 2 of `strcpy' makes pointer from integer without a cast"

 
0
  #1
Apr 22nd, 2005
When trying to compile a C program including the code below, I keep getting this error and I don't know what to do to fix it. Please explain if you can!

Complete code may be downloaded from my website at www.atrixnet.com/cquestion.zip (It's only 2 Kb)

I suppose I'll attach it to this post too, since that appears to be an available feature on this board

COMPILATION ATTEMPT:
tommy@city-it ~/cdevel
$ gcc -Wall -ansi -pedantic -mno-cygwin -I . -o myfirstlib myfirstlib.c

ERROR:
myfirstlib.c: In function `main':
myfirstlib.c:14: warning: passing arg 2 of `strcpy' makes pointer from integer without a cast
myfirstlib.c:20: warning: passing arg 2 of `strcpy' makes pointer from integer without a cast

CODE SUMMARY (main program):
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <myfuncs.h>
  4.  
  5. int anumber;
  6. char astring[255];
  7.  
  8. int
  9. main ()
  10. {
  11. printf ("This is the first time I've tried to '#include' my library!\n\n");
  12.  
  13. anumber = getnum("Enter some kind of integer: ");
  14. strcpy(astring, getstring("Enter your name: "));
  15.  
  16. printf("\nThank you, %s. You entered %d\n", astring, anumber);
  17.  
  18. while (getyn("\nDo you want to go again? [Y/n]")) {
  19. anumber = getnum("Enter some kind of integer: ");
  20. strcpy(astring, getstring("Enter your name: "));
  21.  
  22. printf("\nThank you, %s. You entered %d\n", astring, anumber);
  23. }
  24.  
  25. printf("\nBye.\n");
  26.  
  27. return (0);
  28. }

CODE SUMMARY (from myfuncs.h):
  1. /* will act as the buffer into which user's response will be stored */
  2. char lineofinput[255];
  3.  
  4. /* grabs a string from the command prompt */
  5. char
  6. getstring (char strquestion[255])
  7. {
  8. /* reset */
  9. char lineofinput[255];
  10.  
  11. if (strlen (strquestion))
  12. printf ("%s", strquestion);
  13.  
  14. fgets (lineofinput, sizeof (lineofinput), stdin);
  15.  
  16. /* while we get an empty response */
  17. while (strlen (lineofinput) == 1)
  18. {
  19. if (strlen (strquestion) == 1)
  20. printf ("Empty answers not permitted. Please try again: ");
  21.  
  22. return (getstring (lineofinput));
  23. }
  24.  
  25. return (sprintf("%s", lineofinput));
  26. }

MY COMPILER:
tommy@city-it ~/cdevel
$ gcc -v
Reading specs from /usr/lib/gcc-lib/i686-pc-cygwin/3.3.3/specs
Configured with: /gcc/gcc-3.3.3-3/configure --verbose --prefix=/usr --exec-prefi
x=/usr --sysconfdir=/etc --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --enable-languages=c,ada,c++,d,f77,java,objc,
pascal --enable-nls --without-included-gettext --enable-libgcj --with-system-zlib --enable-interpreter --enable-threads=posix --enable-java-gc=boehm --enable-sjlj-exceptions --disable-version-specific-runtime-libs --disable-win32-registry
Thread model: posix
gcc version 3.3.3 (cygwin special)
Attached Files
File Type: zip cquestion.zip (1.2 KB, 2 views)
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,336
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: total newb - "passing arg 2 of `strcpy' makes pointer from integer without a cast

 
0
  #2
Apr 22nd, 2005
Well, your getstring returns a single char and not a char*.
/* grabs a string from the command prompt */
char
getstring (char strquestion[255])
{
  /* reset */
  char lineofinput[255];

  if (strlen (strquestion))
    printf ("%s", strquestion);

  fgets (lineofinput, sizeof (lineofinput), stdin);

  /* while we get an empty response */
  while (strlen (lineofinput) == 1)
    {
      if (strlen (strquestion) == 1)
         printf ("Empty answers not permitted.  Please try again: ");

      return (getstring (lineofinput));
    }

  return (sprintf("%s", lineofinput));
}
But what you attempt to return is more worrisome.

You may want to take a peek at this.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 8
Reputation: atrixnet is an unknown quantity at this point 
Solved Threads: 0
atrixnet's Avatar
atrixnet atrixnet is offline Offline
Newbie Poster

Re: total newb - "passing arg 2 of `strcpy' makes pointer from integer without a cast

 
0
  #3
Apr 23rd, 2005
Originally Posted by Dave Sinkula
Well, your getstring returns a single char and not a char*.
Yes but how do I change that?

But what you attempt to return is more worrisome.
I'm sorry? What do you mean?

You may want to take a peek at this.
Unfortunately that isn't providing me with any answers to the question I've asked. Remember I'm a total newbie? Please help me out here by guiding me with a little easier terms to understand.

Thanks for responding to me!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,602
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: total newb - "passing arg 2 of `strcpy' makes pointer from integer without a cast"

 
0
  #4
Apr 23rd, 2005
>Yes but how do I change that?
I would think it would be obvious:
char *
getstring (char strquestion[255])
>I'm sorry? What do you mean?
sprintf returns the number of characters printed, not a string of any sort, which is what your use of getstring suggests. You could return lineofinput, and that would satisfy the type system, but then you would have problems because lineofinput is a local variable and is destroyed when the function returns. A much better solution would be to pass a pointer to the buffer to getstring. Then you can return a pointer to the buffer with no problem:
  1. #include <stdio.h>
  2.  
  3. char *getstring ( char buffer[], int size )
  4. {
  5. if ( fgets ( buffer, size, stdin ) == NULL )
  6. return NULL;
  7.  
  8. return buffer;
  9. }
  10.  
  11. int main ( void )
  12. {
  13. char buffer[BUFSIZ];
  14.  
  15. if ( getstring ( buffer, sizeof buffer ) != NULL )
  16. printf ( "%s\n", buffer );
  17.  
  18. return 0;
  19. }
>Unfortunately that isn't providing me with any answers to the question I've asked.
Perhaps not the question you asked now. If we give you a link (Dave or I in particular), we think you need the information whether you asked for it or not.

>Please help me out here by guiding me with a little easier terms to understand.
You read book. Use reference. It help make code good.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 8
Reputation: atrixnet is an unknown quantity at this point 
Solved Threads: 0
atrixnet's Avatar
atrixnet atrixnet is offline Offline
Newbie Poster

Re: total newb - "passing arg 2 of `strcpy' makes pointer from integer without a cast

 
0
  #5
Apr 23rd, 2005
>>Yes but how do I change that?
>I would think it would be obvious:

Seriously? I just started messing with C a few days ago. I've only ever programmed in Perl and Python. The example you provided works but I still have no idea why... I'll continue to research it, but I thought the point of this board was to get help with things, not get cryptic responses that provide more questions than I started with.

So far I'm very disappointed with the quality of help I've received. It's showing me how weak the community is around C and C++ whereas the community around python and perl leaves you guys looking like... well... words that shouldn't be uttered in public foura.

And please don't tell me, "well if you love perl so much why don't you just go away"... That is so below what I hoped you'd prove to be -helpful, friendy.

char *
getstring (char strquestion[255])
Like I said -works great but means nothing to me.

>sprintf returns the number of characters printed, not a string of any sort, which is what your use of getstring suggests. You could return lineofinput, and that would satisfy the type system, but then you would have problems because lineofinput is a local variable and is destroyed when the function returns. A much better solution would be to pass a pointer to the buffer to getstring. Then you can return a pointer to the buffer with no problem:
  1. #include <stdio.h>
  2.  
  3. char *getstring ( char buffer[], int size )
  4. {
  5. if ( fgets ( buffer, size, stdin ) == NULL )
  6. return NULL;
  7.  
  8. return buffer;
  9. }
  10.  
  11. int main ( void )
  12. {
  13. char buffer[BUFSIZ];
  14.  
  15. if ( getstring ( buffer, sizeof buffer ) != NULL )
  16. printf ( "%s\n", buffer );
  17.  
  18. return 0;
  19. }
Thanks this is good information, but still doesn't /explain/ anything, like what the "*" does and where you get "BUFSIZ".

>>Unfortunately that isn't providing me with any answers to the question I've asked.
>Perhaps not the question you asked now. If we give you a link (Dave or I in particular), we think you need the information whether you asked for it or not.

OK.

>>Please help me out here by guiding me with a little easier terms to understand.
>You read book. Use reference. It help make code good.

I really can't believe you just said that. I'm already working out of a book. Dare I ask if you know of any other places I could receive better help with C than this?
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 8
Reputation: atrixnet is an unknown quantity at this point 
Solved Threads: 0
atrixnet's Avatar
atrixnet atrixnet is offline Offline
Newbie Poster

Re: total newb - "passing arg 2 of `strcpy' makes pointer from integer without a cast

 
0
  #6
Apr 23rd, 2005
Originally Posted by atrixnet
where you get "BUFSIZ"
Wait, sorry I forgot BUFSIZ is in the faq you showed me, but not "*".
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 108
Reputation: prog-bman is an unknown quantity at this point 
Solved Threads: 3
prog-bman prog-bman is offline Offline
Junior Poster

Re: total newb - "passing arg 2 of `strcpy' makes pointer from integer without a cast

 
0
  #7
Apr 23rd, 2005
"*" are pointers they point to a memory address.
Dave and Narue are not trying to be mean and cryptic. They are just trying to get you to work things out on your own. Most of us fell that it is a disservice to tell you everything and you not find it out on your own.
Join me on IRC:
Server: irc.daniweb.com
Channel: #C++

Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 8
Reputation: atrixnet is an unknown quantity at this point 
Solved Threads: 0
atrixnet's Avatar
atrixnet atrixnet is offline Offline
Newbie Poster

Re: total newb - "passing arg 2 of `strcpy' makes pointer from integer without a cast

 
0
  #8
Apr 23rd, 2005
Am I to understand then that there's no way for a function to return a true string in C

aka - return("Hello, my name is Simon");
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 108
Reputation: prog-bman is an unknown quantity at this point 
Solved Threads: 3
prog-bman prog-bman is offline Offline
Junior Poster

Re: total newb - "passing arg 2 of `strcpy' makes pointer from integer without a cast"

 
0
  #9
Apr 23rd, 2005
Of course there is. Like dave and prelude saud you have to use the char* type.
e.g
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. char *getSomeString(void);
  5.  
  6. int main(void)
  7. {
  8. char mainString[strlen(getSomeString()) + 1];
  9. strcpy(mainString,getSomeString());
  10. printf("%s",mainString);
  11. getchar();
  12. }
  13.  
  14. char *getSomeString(void)
  15. {
  16. return "Some String";
  17. }
Now I think I did the printf right I am a C++ man
Join me on IRC:
Server: irc.daniweb.com
Channel: #C++

Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 8
Reputation: atrixnet is an unknown quantity at this point 
Solved Threads: 0
atrixnet's Avatar
atrixnet atrixnet is offline Offline
Newbie Poster

Re: total newb - "passing arg 2 of `strcpy' makes pointer from integer without a cast

 
0
  #10
Apr 23rd, 2005
Originally Posted by prog-bman
Of course there is. Like dave and prelude saud you have to use the char* type.
e.g
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. char *getSomeString(void);
  5.  
  6. int main(void)
  7. {
  8. char mainString[strlen(getSomeString()) + 1];
  9. strcpy(mainString,getSomeString());
  10. printf("%s",mainString);
  11. getchar();
  12. }
  13.  
  14. char *getSomeString(void)
  15. {
  16. return "Some String";
  17. }
Now I think I did the printf right I am a C++ man
Ok but i'm still in the dark, I'm sorry. If you say char *getSomeString... you aren't really returning a string, you're returning a pointer right? DOH!

And is it bad to say...
  1. char mainString[] = getSomeString();

My Oreilly book says...
C has a special shorthand for initializing strings: Surround the string with double quotes to simplify initialization...
  1. char name[] = "Sam";
The dimension of name is 4 because C allocates a place for the '\0' character that ends the string.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC