1

when i'm tryin' to use malloc with string pointers to scan, it's givin' a segmentation fault

main(){ char *a; a = (char)malloc(50); printf("enter a string\t"); scanf("%s", a); printf("%s\n", a); } 
5
  • 1
    1. specify the return type of main, 2. specify the arguments main expects (ie void) 3. Remove the cast! 4. return 0; should be the last statement in main 5. Consider using the stack, not the heap for something this trivial Commented Feb 7, 2014 at 16:08
  • i tried all of 'em and it's still givin' segmentation fault Commented Feb 7, 2014 at 16:24
  • @user3248186 The hints from Elias Van Ootegem are just tipps to improve your C. They have nothing to do with your problem. Commented Feb 7, 2014 at 16:39
  • @pzaenger: It does solve the problem, if OP removes the cast, and the input is less than 50 chars, the code works (tried it myself)... to the OP: post the code you tried, following my advise Commented Feb 7, 2014 at 16:41
  • @EliasVanOotegem: Oops, I see, you are right. Haven't seen, that it is a wrong cast. Sorry :) Commented Feb 7, 2014 at 16:42

2 Answers 2

4
a = (char)malloc(50); 

Here, you meant to cast its type to char * instead of char.

Note that it's better not to cast the return type of malloc. See Do I cast the result of malloc?

Sign up to request clarification or add additional context in comments.

1 Comment

Actually it is not better: you simply don't cast the result of malloc. However, the OP should read the accepted answer in your given link.
2

Your main problem, apart from your using scanf is that you're casting a pointer (which is size 4 or 8 on 32 or 64 bit systems respectively) to a char, which is guaranteed to be size 1 as per standard.
Do not ignore compiler warnings. You should've seen something along the lines of:

warning: cast from pointer to integer of different size [-Wpointer-to-int-cast] a = (char) malloc(10); warning: assignment makes pointer from integer without a cast [enabled by default] a = (char) malloc(50); 

Casting a pointer type to char, only to assign it to a pointer variable makes no sense. If you want to make it absolutely clear that you are allocating enough memory to accomodate N chars, then you could write:

a = malloc(50 * sizeof(char)); //or better still a = malloc(50 *sizeof *a);//50 times the size of whatever type a is pointing to... 

But a char is always size 1, like I said before. Anyway: I set about fixing the code you posted:

The following copy-pasted code works just fine, provided you don't enter more than 49 chars:

#include <stdio.h> #include <stdlib.h> int main( void ) { char *a = malloc(50); if (a == NULL) return EXIT_FAILURE;//<-- no memory available printf("enter a string\t"); scanf("%s", a); printf("%s\n", a); free(a);//<== be tidy, free your memory, though here, it's a bit redundant return 0; } 

But really, look into alternatives to scanf, and never cast the pointer returned by malloc in C. C++ is a different matter.
Also, check for NULL pointers, to be safe.

Lastly: Don't ignore compiler warnings.

copy-paste & test here

you can provide stdin input at the bottom right hand side of the page, it'll print the string you provided

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.