The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
String array in C programming
Discuss String array in C programming in the C Programming forum on Dev Shed. String array in C programming C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 3rd, 2013, 10:45 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 6
Time spent in forums: 1 h 1 m 58 sec
Reputation Power: 0
|
|
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??
|

January 3rd, 2013, 10:54 PM
|
 |
Contributed User
|
|
|
|
|
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.
|

January 3rd, 2013, 11:19 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 6
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.
|

January 3rd, 2013, 11:36 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|