473,399 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Storage strings in an array

2
Hi all:

I am new here as in C.
I want to store some strings in an array and i did, but when I show in the screen
only shows me the last string i introduce, here is my C source code:

#include "stdio.h"
#define LENG 45;
char *readString(char *string,int n); // function where read a string and return it
int main(void){
char string[LENG+1] , *ptrString[3];
int conunter;

// Here i am storing

for(counter=0;counter<3;counter++){
printf("String %d: ",counter+1);
ptrString[counter]=readS(string,LENG);//Storing in my string array
}

//Here i send to print in screen the array of strings

for(counter=0;counter<3;counter++){
printf("String[%d]: %s\n",counter+1,ptrString[counter]);
}

return 0;
}

//Here is readS

char *readS(char *string, int n){
char c;
int i=0;
while(1){
car=getchar();
if(car==' \n ' || i>n ) break;
string[i]=car;
i++;
}
string[i]=' \0 ' ;
return string;
}
/*
ok if i make this

String 1: Hi
String 2: how
String 3: are

show this

String 1: are
String 2: are
String 3: are
*/

Please help me.
Krann.
Jul 22 '06 #1
2 3351
D_C
293 100+
I've had that happen before, and the reason why is because I did not properly declare the amount of memory needed.

After reading your code, it looks like you are passing and returning the address to the same variable. In effect, all three pointers in ptrString are all pointing to char string[46] that you have declared. You are overwriting the string itself.

To remedy, you need to declare a new character string in the readS subroutine and return that instead of the pointer to char string[46] that you pass into the routine.
Jul 22 '06 #2
Krann
2
Thanx so much D_C.
Jul 22 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: aneesh | last post by:
Hi all, I have a program, this works fine but if we declare static below "int i" it shows different storage class specifier. what will be the reason. #include <stdlib.h> static int i ; int...
10
by: Ian Todd | last post by:
Hi, I am trying to read in a list of data from a file. Each line has a string in its first column. This is what i want to read. I could start by saying char to read in 1000 lines to the array( i...
13
by: S.Tobias | last post by:
I'm examining the existence of temporary objects by looking at their addresses. The trick is to create a structure that contains an array as its first member. In an expression the array rvalue...
9
by: CptDondo | last post by:
I am working on an embedded platform which has a block of battery-backed RAM. I need to store various types of data in this block of memory - for example, bitmapped data for control registers,...
10
by: Duncan M Gunn | last post by:
Hi, I need to store the following matrix of values: T U V A 2 1 1 B 2 - - C - - 2 Where (-) is an empty cell.
17
by: Christoph Scholtes | last post by:
Hi, I have two questions about the following code snippet. I am trying to read in a series of strings and save them to character arrays. Since I dont know how long my string is going to be (and...
7
by: StupidScript | last post by:
>From the manual "Storage Requirements": "ENUM('value1','value2',...) =1 or 2 bytes, depending on the number of enumeration values (65,535 values maximum)" This seems to mean: "a" = 1 byte...
14
by: Richard Harter | last post by:
Apologies for the length - this post is best viewed with fixed font and a line width >= 72. Below is the source code for a C header file that provides a suite of storage management macros. I am...
13
by: GeezerButler | last post by:
Is an object reference stored as a 32 bit value? In other words, is a simple read or write of an object reference an atomic statement?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.