473,326 Members | 2,147 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,326 software developers and data experts.

unable to store as array of strings

Hi ,

I am unable to store the words in a string as array of strings.Could any body comment on this?

void func(char *s) {
int length;
char c;
char **a;
int i,j,l;
int k=0,count=0;
length=strlen(s);
a=(char **)malloc(2*sizeof(char *));
for(i=0;i<2;i++)
a[i]=(char *)malloc(5);
for(i=0;i<length;i++) {
c=s[i];
if(c==' ') {
j=i-1;
for(l=j;l>=k;l--)
a[count][l]=s[l];
a[count][j-k+1]='\0';
printf("%s\n",a[count]);
k=i;
count++;
}
}
for(i=0;i<count;i++) {
printf("%s\n",a[i]);
}
}


int main() {
char *s="siva kumar";
printf("The given string is %s\n",s);
func(s);
return 0;
}
Jan 22 '08 #1
3 1508
gpraghuram
1,275 Expert 1GB
Hi,
the problem is in ur logic in this part

Expand|Select|Wrap|Line Numbers
  1. for(l=j;l>=k;l--) 
  2. a[count][l]=s[l];
  3.  
  4.  
In the first iteration u will get the string properly.
In the second iteration u start filling the string from the value 4/5 not from 0( with the example u have given)
Try to change this logic

NOTE:Please use code tags while posting

Raghuram
Jan 22 '08 #2
Hi,
the problem is in ur logic in this part

Expand|Select|Wrap|Line Numbers
  1. for(l=j;l>=k;l--) 
  2. a[count][l]=s[l];
  3.  
  4.  
In the first iteration u will get the string properly.
In the second iteration u start filling the string from the value 4/5 not from 0( with the example u have given)
Try to change this logic

NOTE:Please use code tags while posting

Raghuram
Hi Raghu,

in each interation iam going from back to front of a word while storing into a array of strings form.
Jan 22 '08 #3
gpraghuram
1,275 Expert 1GB
Hi Raghu,

in each interation iam going from back to front of a word while storing into a array of strings form.

I understood the logic.
But while getting the second string you are start filling the value from the end of the string to the position 5(That is is l value is 5).
U are doing a memory overwrite.

Raghuram
Jan 23 '08 #4

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

Similar topics

4
by: Mike Dole | last post by:
I'm working on a client - server application based on the 'How to Sockets Server and How to Sockets Client' code from the Visual Basic ..NET Resource Kit. Since I want to be able to send 'big...
6
by: steveneng | last post by:
C++ Primer Plus Programming Exercises 4th Ed - Prate Help I'm trying to refresh myself and I'm stuck on this problem (not homework/school related but for personal advancement). 6: Do...
7
by: news.hku.hk | last post by:
is there any way to make a loop to store strings?? e.g. abc.txt contains 3 lines i.e. this is line 1. this is line 2. this is line 3. i want to create a loop that can make the three strings...
12
by: arkobose | last post by:
my earlier post titled: "How to input strings of any lengths into arrays of type: char *array ?" seems to have created a confusion. therefore i paraphrase my problem below. consider the...
4
by: Robert P. | last post by:
I can easily store a one-dimensional array in viewstate ( see Test1 ) If I try storing a multi-dimensional array in the viewstate it's crapping out on me when it goes to serialize the array (not...
4
by: nass | last post by:
hello everyone, i have a bit of problem reading char * strings from a buffer (a shared memory, pointed to by 'file_memory'). basically i have a structure in memory 'ShMem' that can be accessed by...
7
by: Milsnips | last post by:
Hi there, Can anyone help me out on this one? i want to read each line entered in a multiline textbox and store it into an array. I cant find any properties for this in the control, the only...
0
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
3
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.