Question:
Using pointers in C programming?
verg70
2008-12-01 14:24:51 UTC
Hi, I declare a pointer using

char pName_a;

and then I use scanf to assign a value to my pointer. Next I send my stored value to a function to be used:

computer(&pName_a);

when I try to print the value passed to my function to the screen, I just get jumbled characters. Any help would be appreciated. Thanks
Five answers:
covinher
2008-12-01 14:31:37 UTC
char pName_a; is not a pointer

char* pName_a; is a pointer



You need to allocate space, and set the pointer to point to it, before you can provide its address as an argument to scanf.



Finally, your &pName_a is probably a workaround for your previous mistake -- no ampersand should be required.
Shariq (http://coinsindia.info)
2008-12-01 20:52:09 UTC
I think your basic concept about pointers is not clear. Read the following



1. Pointer is a variable which holds the address of another variable.



You are trying to directly assign value to the pointer which is wrong



check following



int a; //declares a variable

a= 10; //Stores 10 to a

int *p ;// a pointer to point the integer type variable

p=&a // address of variable a stores in p



printf("%d", *p); //Prints value at address stored in p i.e 10





Visit the given link for more details
Tobekeru
2008-12-01 14:36:55 UTC
Pointers are a special kind of vars, that point to memory addresses.



So basically, they need to be declared in a different way.



Therefore the parameter that will be received on the computer function should be:



return_type computer(char *p)



And then, you can pass over the memory address of pName_a through the function like so:



computer(&pName_a); //assuming pName_a isn't a pointer



Also, to print the values you simply need to:



printf("%c",*p);



Also:

char *pointer_name; //pointer declaration



char pName_a; //normal char var declaration



Hope this helps.
has_infoooo
2008-12-01 14:33:51 UTC
hi

many cases can be seen :

does the pointer declared as global

does the print in the function computer or out ?

try to put the main part of program to see and help.

bye for now
wiz_kid 2006
2008-12-01 14:30:13 UTC
It will be better if u can put in the whole program...


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...