C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 26th, 2012, 11:22 AM
arvindeepak arvindeepak is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 1 arvindeepak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m 56 sec
Reputation Power: 0
Input through pointers

I have a program getting input through pointers... It doesn't work in terminal in linux(gcc), but it works when coded in code blocks and in tc...

The problem is that it works fine if there is clrscr statement, cant understand what the logic is, have checked in two pc's... Here is the code...

#include<stdio.h>
void main()
{
char *p;
p="hello";
clrscr();
scanf("%s",p);
printf("%s",p);
}

If there is no clrscr it shows null pointer assignment...

Reply With Quote
  #2  
Old August 26th, 2012, 01:27 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 19 h 25 m 12 sec
Reputation Power: 1774
On what basis did you think
scanf("%s","hello");
was a good idea?

> It doesn't work in terminal in linux(gcc), but it works when coded in code blocks and in tc...
Ever since about 1990 (when ISO-C was first standardised), "strings" were defined as being constants, and so could be placed in read-only memory. Being read-only memory, modifications to them were not allowed at the OS level (you get a segfault). Modern compilers on modern operating systems place strings in read-only memory.

GCC (the default compiler which comes with code blocks) used to have a transition flag called -fwritable-strings which would place "strings" in a writeable location (to aid transition of really old programs).
But since release 4 of GCC, this is no longer an option.
http://gcc.gnu.org/gcc-4.0/changes.html

The fact that something works with TC (on DOS) doesn't mean squat. You can write all sorts of rubbish and get away with it on an OS which has no memory protection at all. Toy programs could abuse pointers no end, and it's only when programs got a bit larger that all the crap coding started to produce errors.

Oh, and when posting code, please use [code][/code] tags, so it looks like this when you post.
Code:
#include <stdio.h>
int main ( ) {  // yes, int, really!
  char buff[100];
  fgets( buff, sizeof(buff), stdin );
  printf("%s", buff );
}
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Input through pointers

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap