#include <stdio.h>
#include <stdlib.h>

/*(*void)displayValue(char* );*/

void main()
	char* userInput = (char*)malloc(sizeof(userInput);
	printf("\n\nEnter : ");
	scanf("%s", &userInput); 
	printf("\n\nEntered : %s\n\n",userInput);

I have got core dump by running this code. Please suggest.

You're breaking the rules without CODE-tags

The obvious issue is you're not freeing the memory after usage.

If I remember correctly, when you use the variable to define the size, you have to point to it.

char* userInput = (char*)malloc(sizeof(*userInput);

After you allocate something, you need to verify that it actually did:

if(userInput == NULL) call_emergency_handling_function();

void main() is incorrect. The standard is int main() and must return(0); on success, 1 on failure.

>>void main()

main should never be declared like that. The only acceptable way to declare it is int main() or int main(int argc, char* argv[]); what is userinput? It has not been defined. Your compiler should have produced an error on that line. If you ignored the error than shame on you!

Ancient Dragon : I think you need to think before throwing a comment.

What part of ancient dragon's comment do you find objectionable? There is no legal void main() declaration in C. In fact, as a.d. says there are only two possible legalmain declarations, both returning int. http://c-faq.com/ansi/maindecl.html

Or are you complaining about the mention of the fact that you attempt to use a not yet declared variable userInput. The compiler should have complained about that (and it should have complained about the incorrectly balanced parentheses, too).

I suggest you:

  • Use the code-tags around your code when you post it (I tell it in reverse order so the message parser doesn't hide the tags)
  • Actually post the same code you (try to) compile
  • Pay attention to what the compiler is telling you (though that can be difficult at times: Interpreting obscure compiler messages is a suitable subject at DaniWeb)

>> I have got core dump by running this code
I'm echoing what griswolf said .. post the actual code that gave/gives you the core dump. The code you've posted should not compile with any compiler - because of the missing ')' (line #8).

Anyhow, assuming that your crashing program had the ')', a couple of things ..

How much memory are you actually allocating there, i.e. sizeof(userInput) == ???
How about e.g.

#define SIZE 20
char * userInput = malloc(SIZE);

Then, scanf("%s", & userInput) <-> you are passing in a char ** , whereas "%s" expects a char * So, you must write ..

scanf("%s", userInput);

Note that scanf("%s", some_char_pointer_here); can be just as unsafe as the infamous, to-be-avoided gets() , because it is completely unaware about the size of the destination buffer. A safe alternative is to specify the size, like so ..

#define SIZE 20
char my_buffer[SIZE] = "";

/* Note: 19 instead of 20 */
int res = scanf("%19s", my_buffer);

if(res == 1)
  puts("No luck with scanf() this time ..");

Oh, and maybe see What's wrong with casting malloc's return value?.

I think all the other issues have already been discussed in this thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.