Answer the question
In order to leave comments, you need to log in
What to do if an exception breakpoint is hit during debugging?
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <locale.h>
void get_str ( char *str )
{
int i;
char c = getchar ( );
for ( i = 0; c != EOF && c != '\n'; i++ )
{
str[i] = c;
c = getchar ( );
}
str[i] = '\0';
}
int main ( void )
{
setlocale ( LC_ALL, "RUSSIAN" );
char str = 0, *pStr = &str;
get_str ( pStr );
for ( int i = 0; pStr[i] != '\0'; i++ )
{
printf ( "\n->%c", pStr[i] );
}
free ( pStr );
return 0;
}
Answer the question
In order to leave comments, you need to log in
char str = 0, *pStr = &str;
It's very, very wrong to do that. A string is an array of char in memory. The pointer points to the beginning of this chunk in memory. You take one variable, and then work with it as with an array.
You need to allocate memory for the string. Or a static array, or malloc.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question