C Program Help
Alright, I'm trying to write a program that will take a specified substring out of a string. For example, if the string is testingonetwothree, the program would be able to create another string out of that string using specified parameters. The program I've wrote is…
#include <stdio.h>
char substring (char source[], int start, int count, char result[]) { int i, n;
for ( i = start + 1, n = 0; i < start + 1 + count; ++i, ++n ) {
result[n] = count[i];
if ( count[i] == '\0' )
return result;
}
result[i] = '\0';
return result;
}
int main (void) { char substring (char source[], int start, int count, char result[]);
int i;
char string[] = "Testingonetwothree"; char stwo[81];
substring (string, 3, 4, stwo); printf ("%s is now %s\n", string, stwo ); return 0; }
substring.c: In function 'substring': substring.c:8: error: subscripted value is neither array nor pointer substring.c:9: error: subscripted value is neither array nor pointer substring.c:10: warning: return makes integer from pointer without a cast substring.c:13: warning: return makes integer from pointer without a cast
Also I'm not sure if it's correct to try to store the new char array as I have it, or to set the result array = to the function which will return the desired string. Sorry if that doesn't make sense.
Is this what you are looking for?
#include <stdio.h>
#include <stdlib.h>
char *substring(char *source, int start, int count)
{
[tab]char *result = (char *)malloc( count-start+1 );
[tab]int i;
[tab]for (i=0; start < count; start++, i++)
[tab]{
[tab][tab]result[i] = source[start];
[tab][tab]if (result[i] == '\0')
[tab][tab][tab]return result;
[tab]}
[tab]result[i++] = '\0';
[tab]return result;
[tab]
}
int main()
{
[tab]char string[] = "Testingonetwothree";
[tab]char *stwo;
[tab]stwo = substring( string, 3, 4);
[tab]printf("\'%s\' is now \'%s\'\n", string, stwo);
[tab]free(stwo);
[tab]return 0;
}
Deflogc wrote: Alright, I'm trying to write a program that will take a specified substring out of a string. For example, if the string is testingonetwothree, the program would be able to create another string out of that string using specified parameters. The program I've wrote is…
#include <stdio.h>
char substring (char source[], int start, int count, char result[]) { int i, n;
for ( i = start + 1, n = 0; i < start + 1 + count; ++i, ++n ) {
result[n] = count[i];
if ( count[i] == '\0' )
return result;
}
result[i] = '\0';
return result;
}
int main (void) { char substring (char source[], int start, int count, char result[]);
int i;
char string[] = "Testingonetwothree"; char stwo[81];
substring (string, 3, 4, stwo); printf ("%s is now %s\n", string, stwo ); return 0; }
substring.c: In function 'substring': substring.c:8: error: subscripted value is neither array nor pointer substring.c:9: error: subscripted value is neither array nor pointer substring.c:10: warning: return makes integer from pointer without a cast substring.c:13: warning: return makes integer from pointer without a cast
Also I'm not sure if it's correct to try to store the new char array as I have it, or to set the result array = to the function which will return the desired string. Sorry if that doesn't make sense.
for ( i = start + 1, n = 0; i < start + 1 + count; ++i, ++n ) {
looks like shit.. try splitting it into 2 for loops (1 being nested) like
{
for( n=0; yadaydad)
{
do shit here;
}
}```
also change count[i] to source[count], thats all i can say after 10 sec look at the code, good luck anyway
EDIT: code doesn't keep indents? pos
spyware wrote: Yeah. Nested loops. That'll improve the quality of the code, and speed up execution time. Good thinking!
where does speed of execution come into this thread? he wasn't looking for optimised code. only a minority of coders need optimised code now, and it doesn't look like he'll be coding anything that level just yet.
Since when are nested loops considered poor quality?
it would be nice for constructive critisism(EDIT: (sp?) ) than a mindless cunt flame kthx
substring.c:8: error: subscripted value is neither array nor pointer substring.c:9: error: subscripted value is neither array nor pointer Those errors are because you are using array notation on count, which is not an array, it's an integer. substring.c:10: warning: return makes integer from pointer without a cast substring.c:13: warning: return makes integer from pointer without a cast And these errors are because you are returning "result", where it should be "result[]".
I dont know if there are any semantics errors in there (probably are) because I dont have time to read through it, but those are the reasons for the errors you posted.