Hellow I'm just started whit C.

I'm usig gcc and get this error:

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:

#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.

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?

OK, but why the for loop? You only want the user input once correct?

Yes, I just wante the user input once, but I don't see how I could do it whitout the loop.

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)

A different approach...

#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
*/

Thanks, but I can't get your code to work, first i get this warning in gcc:

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

Then I try your code and get:

[stage@hal test]$ ./test 46
x = 1048576
x = 4194304
relay = 5242880
[stage@hal test]$ ./test 137
x = 131072
x = 524288
x = 8388608
relay = 9043968

Thanks, but I can't get your code to work, first i get this warning in gcc:

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.

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.

Yes, it solved it. Thanks! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.