Strange Characters on terminal - c

Could someone explain me, why I'm getting these strange characters on output?
I got this code:
#include <stdio.h>
#include <string.h>
int main(int argc, char* argv[]){
if(argc!=2){
printf("Incorrect number of arguments\n");
return -1;
}
int lenServer=0;
int i=0;
while(argv[1][i]!=':'){
lenServer++;
i++;
}
char server[lenServer];
memcpy(server,argv[1],lenServer);
printf("%s\n",server);
return 1;
}
I got this on terminal:
pedro#pedro-VirtualBox:~/Desktop/SDMEU$ gcc table_client.c -o table-client
pedro#pedro-VirtualBox:~/Desktop/SDMEU$ ./table-client ola:eu
ola N
pedro#pedro-VirtualBox:~/Desktop/SDMEU$
Shouldn't I just get ola?

put a NUL('\0') to the end of the string.
char server[lenServer+1];
memcpy(server,argv[1],lenServer);
server[lenServer]=0;

Related

Unix environment running C programs

I am programming C programs in a Unix environment. I need to take a number from a user before the program is executed like so:
./program.out 60
How do I store the integer value in the C program?
You can use argv[] to get command line parameters, e.g.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int n;
if (argc != 2) // check that we have been passed the correct
{ // number of parameters
fprintf(stderr, "Usage: command param\n");
exit(1);
}
n = atoi(argv[1]); // convert first parameter to int
// ... // do whatever you need to with `n`
return 0;
}
int main (int argc, char *argv [ ])
{
//your code
}
argv [1] will then have the address of the numeric string which contains the number.
Then you can change this to an int if needed.
It is quite simple to do and I hope I have got your question right. See below:
#include <stdio.h>
int main(int argc, char* argv[])
{
printf("Number of arguments is: %d\n", argc);
printf("The entered value is %s\n", argv[1]);
return 0;
}
And then compile it on Linux as:
gcc file.c
./a.out 32
The program should print the value you require.
Hope this helps.

C: Command line arguments with string

I'm trying to write a program that takes a string as a command line argument and then runs said argument through a function (str_to_int) that takes a string as an input. However, when I try to compile the program, I get a warning saying
initializing 'char *' with an expression of type 'int' [-Wint
conversion]
char* str = atoi(argv[1]);
^ ~~~~~~~~~~~~~
And when I run the program I get a segmentation fault
I've tested the str_to_int a lot so I'm pretty sure that the issue lies with the command line program. Here's the code for it.
#include "hw3.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[])
{
char* str = atoi(argv[1]);
printf("%d\n", str_to_int(str));
return 0;
}
Can anyone tell me what I'm doing wrong? Thanks.
This is all you need, though it will crash if you leave out the command-line argument.
{
printf("%d\n", str_to_int(argv[1]));
return 0;
}
This is more robust:
int main(int argc, char *argv[])
{
if (argc == 1)
printf("missing parameter.");
else
printf("%d\n", str_to_int(argv[1]));
return 0;
}
#include "hw3.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[])
{
char* str = argv[1];
printf("%d\n", str_to_int(str));
return 0;
}
just remove atoi function invocation and it should work

notify-send in C code

How can I call notify-send from C code with a message stored in my string ?
#include <stdlib.h>
int main(int argc, char *argv[])
{
system("mount something somewhere");
system("notify-send message");
return 0;
}
Just send the string as a parameter to system().
For example:
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char command[100], msg[100];
strcpy(command,"notify-send ");
strcpy(msg,"\"Hello World\"");
strcat(command,msg);
system(command);
return 0;
}
#include <stdio.h>
int main(void)
{
system("notify-send Test \"Hello World\"");
return 0;
}

Finding a unique character

My task is to compare some words and to find a character which is not used in both of them. Here is my code. But I'm getting a warning:
[Warning] passing argument 1 of 'ret' makes pointer from integer without a cast [enabled by default].
And when I'm trying to run it it says consolepauser.exe stopped working
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char ret(char a[1][10],char b[3][10])
{
int i,j,p,t;
for (i=0;i<1;i++)
for (j=0;j<10;j++)
for (p=0;p<3;p++)
for (t=0;t<10;t++)
{
if (tolower(a[i][j]==tolower(b[p][t])))
{
p=3;
break;
}
if (p==2)
if (t==9) return tolower(a[i][j]) ;
}
return 'N';
}
int main(int argc, char *argv[]) {
char k[3][10]={"cHaOs","TOP","blAa"};
char b[1][10]={"SomeThIng"};
char q[1][10]={"HaPa"};
if (ret(b[1][10],k[3][10])='N') printf("No character") ;
else printf("%c",ret(b[1][10],k[3][10])) ;
return 0;
}
You should pass the parameters as:
if (ret(b, k) == 'N') printf("No character");
else printf("%c", ret(b, k));
[Warning] passing argument 1 of 'ret' makes pointer from integer without a cast
b[1][10] is a char, not a variable of type char [1][10], you should call ret() like this: ret(b, k). Others are similar.
Note: the valid indexes of char b[1][10]; are b[0][0], b[0][1], ..., b[0][9], the indexes in `b[1][10]1 are out-of-bounds, and will cause undefined behavior.
Here is a syntax fixed version of your code, you may want to compare it with your original code to find out other problems in it:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char ret(char a[1][10],char b[3][10])
{
int i,j,p,t,e,r;
for (i=0;i<1;i++)
for (j=0;j<10;j++)
for (p=0;p<3;p++)
for (t=0;t<10;t++)
{
if (tolower(a[i][j])==tolower(b[p][t]))
{
p=3;
break;
}
if (p==2)
if (t==9) return tolower(a[i][j]) ;
}
return 'N';
}
int main(int argc, char *argv[]) {
int i,j,p,t,e,r;
char a,h;
char k[3][10]={"cHaOs","TOP","blAa"};
char b[1][10]={"SomeThIng"};
char q[1][10]={"HaPa"};
if (ret(b,k)=='N') printf("No character");
else printf("%c",ret(b,k));
return 0;
}

What's wrong in this code?

I was trying to mimic strtok functionality but getting segmentation fault. Please help me out here.
Here is my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char argv[])
{
int i=0;
char c[]="get the hell out of here";
char *p;
char *temp=(char *)malloc(100);
while(c[i]!='\0')
{
if(c[i]!=' ')
{
*temp=c[i];
temp++;
i++;
}
else
{
*temp='\0';
printf("printing tokenn");
puts(temp);
i++;
temp="";
}
}
return 0;
}
temp="";
This causes temp to point at unmodifiable memory, leading to a fault the next time you try to modify through it. You wanted to restore temp to the value you got from malloc (which you forgot to save).

Resources