S
solanki.chandrakant
hi
i have fedora linux 4 and i have simple realloc program which i am
included here... plz help me to overcome the realloc to pointer to
character variable. The program working 2 times but more than 2 times
it gives error : realloc() - invalid next size.
#include <stdio.h>
#include <stdlib.h>
char *source = '\0';
int strlens( char *name ){ /* find the length of given string */
int k = 0;
for( ; name[k] != '\0'; k++ ) ;
return k;
}
int strcats( char *name, int start ) { /* concatenate the string */
int l = strlens( name );
int i = start, j = 0, z;
if( NULL == ( source = realloc ( source, l * sizeof( char ) + 1 )) )
fprintf( stderr, "error: realloc failed.\n" );
l += start;
while( i != l )
source[i++] = name[j++];
source[i++] = '!';
return i;
}
int main( void ) { /* main function */
int length = 0, k;
char *string = "abcdefghijklmnopqrstuvwxyz";
for( k = 0; k < 5; k++ ) {
length = strcats( string, length );
fprintf( stderr, "Length : %d\tString : %s\n", length, source );
}
return 0;
}
Chandrakant
i have fedora linux 4 and i have simple realloc program which i am
included here... plz help me to overcome the realloc to pointer to
character variable. The program working 2 times but more than 2 times
it gives error : realloc() - invalid next size.
#include <stdio.h>
#include <stdlib.h>
char *source = '\0';
int strlens( char *name ){ /* find the length of given string */
int k = 0;
for( ; name[k] != '\0'; k++ ) ;
return k;
}
int strcats( char *name, int start ) { /* concatenate the string */
int l = strlens( name );
int i = start, j = 0, z;
if( NULL == ( source = realloc ( source, l * sizeof( char ) + 1 )) )
fprintf( stderr, "error: realloc failed.\n" );
l += start;
while( i != l )
source[i++] = name[j++];
source[i++] = '!';
return i;
}
int main( void ) { /* main function */
int length = 0, k;
char *string = "abcdefghijklmnopqrstuvwxyz";
for( k = 0; k < 5; k++ ) {
length = strcats( string, length );
fprintf( stderr, "Length : %d\tString : %s\n", length, source );
}
return 0;
}
Chandrakant