| | |
makes pointer from integer without a cast
Thread Solved |
•
•
Join Date: Jun 2005
Posts: 8
Reputation:
Solved Threads: 0
Hellow I'm just started whit C.
I'm usig gcc and get this error:
This is my code:
What is I doing wrong?
I'm usig gcc and get this error:
C Syntax (Toggle Plain Text)
test.c: In function 'main': test.c:8: warning: passing argument 1 of 'strlen' makes pointer from integer without a cast .....
This is my code:
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> int main(int argc, char *argv[]){ int arg = atoi(argv[argc-1]); int relay; int i; for(i = 0; i < strlen(arg); i++) { if (strcmp(arg, "1") == 0){ relay = 1; }else{ if (strcmp(arg, "2") == 0){ relay = relay + 2; }else{ if (strcmp(arg, "3") == 0){ relay = relay + 4; }else{ if (strcmp(arg, "4") == 0){ relay = relay + 8; }else{ if (strcmp(arg, "5") == 0){ relay = relay + 16; }else{ if (strcmp(arg, "6") == 0){ relay = relay + 32; }else{ if (strcmp(arg, "7") == 0){ relay = relay + 64; }else{ if (strcmp(arg, "8") == 0){ relay = relay + 128; } printf("relay %d/n", relay); }}}}}}} } }
What is I doing wrong?
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.
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.
•
•
Join Date: Jun 2005
Posts: 8
Reputation:
Solved Threads: 0
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)
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)
A different approach...
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(int argc, char *argv[]) { int relay = 0; int i; for ( i = 0; argv[1][i]; ++i ) { int x = 1 << (argv[1][i] - '1 '); printf("x = %d\n", x); relay += x; } printf("relay = %d\n", relay); return 0; } /* my output C:\Test>test 46 x = 8 x = 32 relay = 40 C:\Test>test 137 x = 1 x = 4 x = 64 relay = 69 */
"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
•
•
Join Date: Jun 2005
Posts: 8
Reputation:
Solved Threads: 0
Thanks, but I can't get your code to work, first i get this warning in gcc:
Then I try your code and get:
C Syntax (Toggle Plain Text)
test.c:11:34: warning: multi-character character constant
Then I try your code and get:
C Syntax (Toggle Plain Text)
[stage@hal test]$ ./test 46 x = 1048576 x = 4194304 relay = 5242880 [stage@hal test]$ ./test 137 x = 131072 x = 524288 x = 8388608 relay = 9043968
•
•
•
•
Originally Posted by stage
Thanks, but I can't get your code to work, first i get this warning in gcc:
C Syntax (Toggle Plain Text)
test.c:11:34: warning: multi-character character constant
int x = 1 << (argv[1][i] - '1 '); C Syntax (Toggle Plain Text)
int x = 1 << (argv[1][i] - '1');
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
![]() |
Similar Threads
- warning: assignment makes pointer from integer without a cast (C)
- strcmp without cast (C)
- total newb - "passing arg 2 of `strcpy' makes pointer from integer without a cast" (C++)
Other Threads in the C Forum
- Previous Thread: Getting an array from a txt file
- Next Thread: StretchDIBits
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf scripting segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






