pointer to string error

Status
Not open for further replies.

syedshan

Advanced Member level 1
Joined
Feb 27, 2012
Messages
463
Helped
27
Reputation
54
Reaction score
26
Trophy points
1,308
Location
Jeonju, South Korea
Activity points
5,134
Hello all,

I am completely new to the practical programming of C

I have done C-programming during my undergraduate 2 years ago, now that I have to reuse it in one of my program I have restarted its revision...
I have problem with the Pointers to C, as we know that declaring char *p; would create a string pointer.

Hence I typed in the following code and compile it using Visual studio 2010

Code:
#include <conio.h>
#include <stdio.h>
#include <iostream>

int main(void)
{
	char *pta; 
	puts("Enter name : ");
	scanf("%s",pta);
//	gets(name); // I also tried this thing with gets() as well	
}

I am having following error (please see the image as well) "The Variable pta is being used without initialization"
 

Yaa.... you need to allocate some memory to the pointer

try this -
Code:
#include <conio.h>
#include <stdio.h>
#include <iostream>

int main(void)
{
	char *pta; 
         pta = (char *) malloc(100*sizeof(char));
	puts("Enter name : ");
	scanf("%s",pta);
//	gets(name); // I also tried this thing with gets() as well	
}


Good Luck
 
Hey great...

Thanks for help.
It works...

Pardon me if I can ask... malloc(100*sizeof(char)) would aqcuire 100 x 1 Byte memory for us, i.e 100 Byte memory
 

Yes.... you are right... as the char is 1 byte so when you say malloc(100*sizeof(char)) would aqcuire 100 x 1 Byte....

Good Luck
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…