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 January 3rd, 2013, 10:45 PM
Viraj Gamage Viraj Gamage is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 6 Viraj Gamage User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 1 m 58 sec
Reputation Power: 0
Post String array in C programming

I am new to C and getting lot of small problems.This time i tried to get some user inputs to a char array and print them on console.

Code:
#include <stdio.h>
#include <string.h>


typedef char * string;
 
int main(void)
{
  string strs[3];
   int i,j;
  
	for(j=0; j<3; j++){
		scanf("%s", &strs[j]);
	}
   
    for(i=0;i<sizeof(strs)/sizeof(string);++i)
        printf("%s",strs[i]);
   
 
  return 0;
}


problem i get is,when program runs, after giving input strings to the program,a window appears showing program needs to close.
But same program worked fine with integers(using an int array).
Can someone help me solving this problem??

Reply With Quote
  #2  
Old January 3rd, 2013, 10:54 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,835 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 15 h 20 m 42 sec
Reputation Power: 1774
The problem is, you don't have any storage allocated.

Start with something simple, like
char strs[3][100];

before you start obscuring things with a pointer typedef.
__________________
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
  #3  
Old January 3rd, 2013, 11:19 PM
Viraj Gamage Viraj Gamage is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 6 Viraj Gamage User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 1 m 58 sec
Reputation Power: 0
Quote:
Originally Posted by salem
The problem is, you don't have any storage allocated.

Start with something simple, like
char strs[3][100];

before you start obscuring things with a pointer typedef.


Thanks!
It works..But can u explain why should i do so,,I ask this because it worked before with integers without allocating storage.

Reply With Quote
  #4  
Old January 3rd, 2013, 11:36 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,124 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 16 h 47 m 13 sec
Reputation Power: 1949
The int array contained memory assigned to hold the int values. The char* array contains memory assigned to hold pointer values, AKA "addresses". Pointers need to point to something. The pointers in that array contained garbage addresses, just random locations because of what just happened to have been stored in those locations before your program ran. They could point to anywhere, most likely outside the range of memory addresses that your program was given by the operating system. Trying to access locations outside your allotted memory is punishable by the death of your program: it gets terminated with extreme prejudice with a SEGFAULT or ACCESS error. This error you committed is also called "using an uninitialized pointer".

One solution is to declare a 2D array of char. Another is to malloc or calloc space for those strings and assign the resultant pointer values in the char* array, thus initializing them.

A pointer must always be initialized to point to somewhere safe.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > String array in C programming

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