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



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 18th, 2013, 05:39 AM
mrityunjay23 mrityunjay23 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2013
Posts: 1 mrityunjay23 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 26 m 21 sec
Reputation Power: 0
Double pointer final printing final[0] garbaje after allocating memory to final[4]

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int ulen,slen,mergelen;
char **final;
int overlap_last(char *st)
{
int i,k,j,length;
char last;
int overlap_at;
overlap_at=0;
length=strlen(st);
last=st[length-1];
for(k=0;k<=((length-3)/2);k++)
{
if(st[length-1-k-1]==last && st[length-1-k-1-k-1]==last)
{
overlap_at=length-1-k-1;
for(j=0;j<k;j++)
{
if(st[length-1-k-1-k+j]!=st[length-1-k+j])
{
overlap_at=0;
break;
}
}
if(overlap_at!=0)
break;
}
}
return overlap_at;
}
void overlapfree_merge_sets(char **u, int ulen, char **s, int slen)
{
int i,j,k,overlap,len,l,p,duplicate,finallen,length;
char *temp;
char *temp1;
overlap=0;
duplicate=0;
final=(char **)malloc(sizeof(char *)*ulen);
for(i=0;i<ulen;i++)
{
length=strlen(u[i]);
final[i]=malloc((length+1)*sizeof(char));
strcpy(final[i],u[i]);
}
finallen=ulen;
for(i=0;i<ulen;i++)
{
for(j=0;j<slen;j++)
{
length=strlen(u[i]);
temp=malloc((length+1)*sizeof(char));
strcpy(temp,u[i]);
len=strlen(temp);
strcat(temp,s[j]);
overlap=0;
for(k=len+1;k<=strlen(temp);k++)
{
temp1=malloc(sizeof(char)*(k+1));
strncpy(temp1,temp,k);
if(overlap_last(temp1))
{
overlap=1;
free(temp1);
break;
}
free(temp1);
}
//adding overlap free concatenated element of u[i] and s[j]
if(overlap!=1 && k==(strlen(temp)+1))
{
duplicate=0;
for(l=0;l<finallen;l++)
{
if(strcmp(final[l],temp)==0)
{
duplicate=1;
break;
}
else duplicate=0;
}
if(duplicate!=1)
{
length=strlen(temp);
final[finallen]=malloc((length+1)*sizeof(char));
strcpy(final[finallen],temp);
finallen++;
}
}
free(temp);
///adding all element of s
if(i==0)
{
duplicate=0;
for(l=0;l<finallen;l++)
{
if(strcmp(final[l],s[j])==0)
{
duplicate=1;
break;
}
else duplicate=0;
}
if(duplicate!=1)
{
length=strlen(s[j]);
final[finallen]=malloc((length+1)*sizeof(char));
strcpy(final[finallen],s[j]);
finallen++;
}
}
} //s loop
}///u loop
mergelen=finallen;
}
int main()
{
int i,k;
char **u;
char **s;
char **t;
ulen=2;
slen=2;
u=(char **)malloc(sizeof(char *)*ulen);
for(i=0;i<ulen;i++)
{
printf("k=");
scanf("%d",&k);
// u[i]=(char *)malloc(sizeof(char)*(k+1));
u[i]=malloc(sizeof(char)*(k+1));
printf("u[%d]=",i);
scanf("%s",u[i]);
}
printf("\n");
s=(char **)malloc(sizeof(char *)*slen);
for(i=0;i<slen;i++)
{
printf("k=");
scanf("%d",&k);
// s[i]=(char *)malloc(sizeof(char)*(k+1));
s[i]=malloc(sizeof(char)*(k+1));
printf("s[%d]=",i);
scanf("%s",s[i]);
}
overlapfree_merge_sets(u,ulen,s,slen);
for(i=0;i<mergelen;i++)
{
printf("%s\n",final[i]);
}
printf("\n");
return 0;
}

I have changed code.
But it is giving same error.

Mathematical Logic:
Enter set of strings u
Enter set of strings s
Final set:
final= u union s union {xy | x element of u, and y is element of s, and for ALL yi overlap_last(xyi) should be 0, where yi is prefix of y }

The overlap last function is defined in programme.
The overlap_last function is working correct.

When you run above programme enter value like below:

k=1
u[0]=a
k=1
u[1]=b

k=1
s[0]=a
k=1
s[1]=b

output:
Code:

�A� it should be a this was also correct up to finallen=4 whenit changes to 5. a replaced by above value
b
aa
ab other all are correct
ba
bb

if you want to change length of u and s, then change ulen and slen, for above input it is defined as ulen=2 ans slen=2.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Double pointer final printing final[0] garbaje after allocating memory to final[4]


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 - 2018, Jelsoft Enterprises Ltd.

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