I have this code:
#include<stdio.h>
int main(int args,char*argv[])
{
char *input_f;
input_f=(argv+1);
printf("%d\n",input_f);
printf("%d\n",(argv+1));
printf("%s\n",*(argv+1));
printf("%s\n",argv[1]);
printf("%s\n",*input_f);
return 0;
}
But if I run it (test.exe hi.txt) the output will be:
1577880
1577880
hi.txt
hi.txt
(null)
How can I create pointer to point the argv[1]?
And why my code doesn't work?
EDIT: The program takes an file at input and the pointer input_f points it but at the output the argv[1] prints the file name but input_f doesn't!
Use this
input_f=*(argv+1);
instead of
input_f=(argv+1);
and
use this
printf("%s\n", input_f);
instead of
printf("%s\n",*input_f);
Full code:
#include<stdio.h>
int main(int args,char*argv[])
{
char *input_f;
input_f=*(argv+1);
printf("%s\n",input_f);
printf("%s\n",*(argv+1));
printf("%s\n",argv[1]);
printf("%s\n",input_f);
return 0;
}
why my code doesn't work?
Assuming that input_f is declared as:
char *input_f;
the format specifier %s in the printf():
printf("%s\n",*input_f);
expects that given argument is of type char *, you are providing *input_f, which is of type char.
How can I create pointer to point the argv[1]?
argv is of type array of pointer to char. Therefore, argv[1] is of type pointer to char. So, you can store it address in a pointer to pointer to char, i.e.: type char **:
char **p = &argv[1];
Related
I created a string in the main body and I want a function to access it. In order to access it I have to use the name of the string, not the pointer to it:
#include <stdio.h>
void test(char * s,char * b){
printf("%s\n",s);
//printf("%s\n",*s); this crashes it
printf("%c\n",*b);
}
int main(){
char s[]="string";
char b='b';
char * pointerb=&b;
char * pointerstring=&s;
test(pointerstring,pointerb);
}
Why is line 5 correct and not line 6?
printf("%s\n",*s);
%s is a format specifier for string. it expects pointer to array of char, but you are passing argument type is char. this is the reason for segmentation fault.
another side note, your has warning for this line
char * pointerstring=&s;
need to change to below.
char * pointerstring=s;
char* pointerstring now points to first char of string s[] which is nothing but 's'
Here is stack visualization of how pointer point to the string when program reaches at line char * pointerstring=s;
My question is about dereferencing a char pointer
Here is my code -
#define MAX 10
char s[80]="Hello";
int main(){
char *stackValue;
stackValue=&s;//here I assined the address of s to stackValue
if(!stackValue){
printf("No place for Value");
exit(1);
}
else{
printf("\n%s",*stackValue);//This doesn't work with * before it
printf("\n%s",stackValue);//This works properly
}
return 0;
}
In the above code I have assigned the address of S[] to stackValue and when I am printing *stackValue it doesn't work ,
But If I print only 'stackValue' That works.
When I do same thing with Integer
int main(){
int i=10, *a;
a=&i;
printf("%d",*a);//this gives the value
printf("%d",a)//this gives the address
return 0;
}
Is printing char pointer and integer pointer is different. When I use * in int value it gives the value but gives an error when I use it as a char pointer.
Help me out?
With the first code snippet:
stackValue=&s; is incorrect given s is already an array to char. If you write like that then stackValue becomes pointer to pointer to char (not pointer to char).
Fix that by changing to stackValue=s;
Also, again %s expect a pointer to char (NOT pointer to pointer to char) - that explains why this doesn't work
printf("\n%s",*stackValue); // this doesn't work
You need printf("\n%s",stackValue); instead.
With the second code snippet.
a=&i; is ok because i is a single int, NOT an array.
What you are trying to do is this:
int main(void)
{
char a_data = "Hello, this is example";
char *pa_stack[] = {a_data};
printf("We have: %s\n", *pa_stack);
}
The "%s" format specifier for printf always expects a char* argument.
so this is working and correct statement
printf("\n%s",stackValue);
and in first statement you are passing value so it will give you undefined behaviour.
I have this C function:
void sysexCallback(byte command, byte argc, byte *argv)
{
...
}
and I want to convert argv[0] (binary) to simple string. I tried things like:
char v[10];
strcpy(v,argv[0]);
But that gives me an error:
Arduino/hardware/tools/avr/avr/include/string.h:126:14: error: initializing argument 2 of 'char* strcpy(char*, const char*)' [-fpermissive]
strcpy takes two character pointers as its parameters. argv[0] is a byte, not a char pointer. Try
strcpy(v,(char *)argv);
Alternatively you can make a character pointer and point it to the byte pointer like so:
char *string = (char *)argv;
I am starting to learn pointers in C.
Why do I have an error in line 8 at &i?
This is the source:
char * func(char *d, char *str)
{
return d;
}
int main(int argc, char *argv[])
{
char *i = NULL;
func(&i, "aaa"); // line 8. here I have the error (in "&i")
}
You are passing char** and the function expects a char*. You need to pass i without the address of & operator, because this way you are taking the address of the pointer.
Just pass func(i, "aaa");
The type of &i is not char * it is char * *.
You should go through this
How do pointer to pointers work in C?
When you write &i, you are passing the memory address of i, but since i is already a char*, the type of &i is char** (pointer to pointer to char). You therefore have an extraneous & that is causing a type mismatch. Simply remove the & and pass i to the function with taking its address:
func(i, aaa); //no need to use & on a variable that is already a pointer
As per your function prototype func(char *d, char *str) first parameter accepts pointer of char type and in char *i; i is a pointer of char type so pass it directly to function.
If you want to pass &i to the function you need to have pointer to pointer variable that is char **d.
I have program
void alloc(char **p)
{
*p=(char*)malloc(sizeof(char)*3);
(*p)[0]='a';
(*p)[1]='f';
(*p)[2]='\0';
}
main()
{
char p[]="hrrgr";
alloc(&p);
printf("%s",p);
}
It prints nothing. Please explain this.
I know by passing char*p ; and alloc(&p) will do the trick. But the purpose of my question is to understand the output I am getting.
p is an array of 6 characters. Therefore p is of type char[6]. &p is then of type pointer to char[6] type, i.e., char (*)[6]. When you pass an array to a function, it evaluates to a pointer to its first element. Thus when you are passing a char (*)[6] type value to your alloc function, you are assigning a char (*)[6] type to a char ** type. They are incompatible types and have different pointer arithmetic. You can't make char (*)[6] behave like a char ** type, even by typecasting which will only suppress compiler warning.
This is not allowed: p is an array of 6 characters, not a char pointer. You should not treat &p as a char** pointer.
If you want to fix this code, declare p like char*, not as an array:
char *p="hrrgr";
alloc(&p);
Running demo on ideone.
&p is of type char (*)[6] but your function alloc expects an argument of type char **. You are passing wrong type parameter.
try this...
void alloc(char **p)
{
*p=(char*)malloc(sizeof(char)*3);
(*p)[0]='a';
(*p)[1]='f';
(*p)[2]='\0';
}
main()
{
char *p;
alloc(&p);
printf("%s",p);
}
When ever you declare an array then there will be continuous allocation of memory but here your are changing starting 3 elements memory which changes the whole location of array and printing garbage...