makes pointer from integer without a cast

Thread Solved

Join Date: Jun 2005
Posts: 8
Reputation: stage is an unknown quantity at this point 
Solved Threads: 0
stage stage is offline Offline
Newbie Poster

makes pointer from integer without a cast

 
0
  #1
Jun 7th, 2005
Hellow I'm just started whit C.

I'm usig gcc and get this error:
  1. test.c: In function 'main':
  2. test.c:8: warning: passing argument 1 of 'strlen' makes pointer from integer without a cast
  3. .....

This is my code:
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main(int argc, char *argv[]){
  5. int arg = atoi(argv[argc-1]);
  6. int relay;
  7. int i;
  8. for(i = 0; i < strlen(arg); i++)
  9. {
  10. if (strcmp(arg, "1") == 0){
  11. relay = 1;
  12. }else{
  13. if (strcmp(arg, "2") == 0){
  14. relay = relay + 2;
  15. }else{
  16. if (strcmp(arg, "3") == 0){
  17. relay = relay + 4;
  18. }else{
  19. if (strcmp(arg, "4") == 0){
  20. relay = relay + 8;
  21. }else{
  22. if (strcmp(arg, "5") == 0){
  23. relay = relay + 16;
  24. }else{
  25. if (strcmp(arg, "6") == 0){
  26. relay = relay + 32;
  27. }else{
  28. if (strcmp(arg, "7") == 0){
  29. relay = relay + 64;
  30. }else{
  31. if (strcmp(arg, "8") == 0){
  32. relay = relay + 128;
  33. }
  34. printf("relay %d/n", relay);
  35. }}}}}}}
  36. }
  37. }

What is I doing wrong?
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: makes pointer from integer without a cast

 
0
  #2
Jun 7th, 2005
A couple of things:

First, you need to include "stdlib.h" for atoi. (Note, I think atoi is not standard, consider sprintf)

Second, you are trying to get the length of an integer ( strlen(arg). I'm not sure what you're trying to do. Then, you are trying to string compare integers (strcmp( arg, "8"), etc.

Please explain what the program is supposed to do and we'll tell you how to change it.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 8
Reputation: stage is an unknown quantity at this point 
Solved Threads: 0
stage stage is offline Offline
Newbie Poster

Re: makes pointer from integer without a cast

 
0
  #3
Jun 7th, 2005
The program is supposed to take the user argument and transform it to anothe number. Like if the user write 3 the number will be 4 or if the user write 12 the number will be 3 or if the user write 12345678 the number will be 255, got it?
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: makes pointer from integer without a cast

 
0
  #4
Jun 7th, 2005
OK, but why the for loop? You only want the user input once correct?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 8
Reputation: stage is an unknown quantity at this point 
Solved Threads: 0
stage stage is offline Offline
Newbie Poster

Re: makes pointer from integer without a cast

 
0
  #5
Jun 7th, 2005
Yes, I just wante the user input once, but I don't see how I could do it whitout the loop.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 8
Reputation: stage is an unknown quantity at this point 
Solved Threads: 0
stage stage is offline Offline
Newbie Poster

Re: makes pointer from integer without a cast

 
0
  #6
Jun 7th, 2005
Anyone know how I can do this:
I want to take the user argument (argv[argc-1]), which is a number in one or several digts (1 to 8 digts) and transform it to another number.... Accordig to this table:
1 = 1
2 = 2
3 = 4
4 = 8
5 = 16
6 = 32
7 = 64
8 = 128

So if the user write 46 the number will be 40 (8+32) or if the user write 137 the number will be 69 (1+4+64)
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,359
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: 239
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: makes pointer from integer without a cast

 
0
  #7
Jun 7th, 2005
A different approach...
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7. int relay = 0;
  8. int i;
  9. for ( i = 0; argv[1][i]; ++i )
  10. {
  11. int x = 1 << (argv[1][i] - '1 ');
  12. printf("x = %d\n", x);
  13. relay += x;
  14. }
  15. printf("relay = %d\n", relay);
  16. return 0;
  17. }
  18.  
  19. /* my output
  20. C:\Test>test 46
  21. x = 8
  22. x = 32
  23. relay = 40
  24.  
  25. C:\Test>test 137
  26. x = 1
  27. x = 4
  28. x = 64
  29. relay = 69
  30. */
"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: Jun 2005
Posts: 8
Reputation: stage is an unknown quantity at this point 
Solved Threads: 0
stage stage is offline Offline
Newbie Poster

Re: makes pointer from integer without a cast

 
0
  #8
Jun 7th, 2005
Thanks, but I can't get your code to work, first i get this warning in gcc:

  1. test.c:11:34: warning: multi-character character constant

Then I try your code and get:

  1. [stage@hal test]$ ./test 46
  2. x = 1048576
  3. x = 4194304
  4. relay = 5242880
  5. [stage@hal test]$ ./test 137
  6. x = 131072
  7. x = 524288
  8. x = 8388608
  9. relay = 9043968
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,359
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: 239
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: makes pointer from integer without a cast

 
0
  #9
Jun 7th, 2005
Originally Posted by stage
Thanks, but I can't get your code to work, first i get this warning in gcc:

  1. test.c:11:34: warning: multi-character character constant
int x = 1 << (argv[1][i] - '1 ');
Hmm. How'd that extra space get in there? It should be like this.
  1. int x = 1 << (argv[1][i] - '1');
I don't know if that solves the issue. But do you see generally what the code was trying to do?

That is, take each character of the number entered. Subtracting '1' will from '1' - '8' will give a value from 0 - 7. Left shifting a 1 by this amount should calculate a value like your table. The loop just sums 'em up.
"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: Jun 2005
Posts: 8
Reputation: stage is an unknown quantity at this point 
Solved Threads: 0
stage stage is offline Offline
Newbie Poster

Re: makes pointer from integer without a cast

 
0
  #10
Jun 7th, 2005
Yes, it solved it. Thanks!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC