LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-14-2006, 10:52 AM   #1
duryodhan
Senior Member
 
Registered: Oct 2006
Distribution: Slackware 12 Kernel 2.6.24 - probably upgraded by now
Posts: 1,054

Rep: Reputation: 46
is malloc necessary for char *s?


Will the following work :

char *s;
scanf("%s",s);
printf("%s",s);


IT seems to work on some comps and not on some.. what is the reason?
 
Old 11-14-2006, 10:59 AM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
IT seems to work on some comps and not on some.. what is the reason?
Uninitialised pointer.
the following could cause a buffer overrun but try
Code:
#define ARRAY_SIZE 20
char s[ARRAY_SIZE];
scanf("%s",s);
printf("%s",s);

Last edited by dmail; 11-14-2006 at 11:02 AM.
 
Old 11-14-2006, 12:48 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Yes - of COURSE you need to allocate space for a string!

duryodhan -

Yes, "allocation" is necessary.

And no, you don't necessarily need "malloc()" just to allocate space. As DMail's example shows.

Here are three equivalent examples:
Code:
#include <stdio.h>
#include <string.h>
#include <malloc.h>

#define MAXCHAR 20

char *
trim (char *s)
{
  s[strlen(s)-1] = '\0';
  return s;
}

void
get_value (char *s, int maxlen)
{
  fgets (s, maxlen, stdin);
  printf ("s= %s...\n", trim(s));
}

int
main (int argc, char *argv[])
{
  char *s1 = NULL, *s2 = NULL, s3[MAXCHAR+1];

  s1 = (char *)malloc (MAXCHAR+1);
  if (s1)
  {
    get_value (s1, MAXCHAR);
    free (s1);
    s1 = NULL;
  }

  s2 = new char[MAXCHAR+1];
  if (s2)
  {
    get_value (s2, MAXCHAR);
    delete[] s2;
    s2 = NULL;
  }

  get_value (s3, sizeof (s3));

  return 0;
}
Here's a sample compile/execute:
Quote:
g++ -g -o x x.cpp
./x
abc
s= abc...
def
s= def...
123
s= 123...
'Hope that helps .. PSM
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
invalid conversion from `const char*' to `char*' deepinlife Programming 22 08-05-2006 10:49 AM
If I get invalid conversion from `const char*' to `char' what should I be lookin for? RHLinuxGUY Programming 5 03-12-2006 10:35 PM
char malloc + uknown characters alaios Programming 3 09-08-2005 10:40 AM
invalid conversion from `char' to `const char* bru Programming 6 05-09-2004 03:07 PM
convert from char* to unsigned char* D J Linux - Software 2 02-20-2004 04:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration