Follow us on Twitter!
Things are more like they are now than they have ever been before. - Dwight D. Eisenhower
Thursday, April 17, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 20
Guests Online: 18
Members Online: 2

Registered Members: 82822
Newest Member: TheBunter
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

C Program Help


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 03-11-08 07:02
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.

Edited by on 03-11-08 07:02
Author

RE: C Program Help


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 03-11-08 08:02
Is this what you are looking for?

Code

#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;
}








Edited by on 03-11-08 08:13
Author

RE: C Program Help


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 03-11-08 22:46
Yeah, that would probably work. While I could use pointers, I got this challenge in a book before pointers were introduced. Pointers is the chapter I'm reading now, but I would like to write it without them, since that was the intent.
Author

RE: C Program Help


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 03-11-08 22:53
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

Code
for ( i = start+1; yadayada )
{
     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

Edited by on 03-11-08 22:54
Author

RE: C Program Help

spyware
Member



Posts: 4192
Location: The Netherlands
Joined: 14.04.07
Rank:
God
Warn Level: 90
Posted on 03-11-08 23:01
Yeah. Nested loops. That'll improve the quality of the code, and speed up execution time. Good thinking!




img507.imageshack.us/img507/3580/spynewsig3il1.png
"The chowner of property." - Zeph
[small]
Widespread intellectual and moral docility may be convenient for leaders in the short term,
but it is suicidal for nations in the long term.
- Carl Sagan
“Since the grid is inescapable, what were the earlier lasers about? Does the corridor have a sense of humor?” - Ebert
[/s
http://bitsofspy.net
Author

RE: C Program Help


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 03-11-08 23:07
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

Edited by on 03-11-08 23:17
Author

RE: C Program Help

ynori7
Member



Posts: 1486
Location: #valhalla
Joined: 08.10.07
Rank:
God
Posted on 04-11-08 00:22
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.


halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg
ynori7 http://halls-of-valhalla.org
Author

RE: C Program Help


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 04-11-08 01:19
ynori7 thanks, that was the problem and it now works how I intended. Sorry for wasting your guys time with such dumb mistakes, but sometimes you get tired at looking at the same thing and not seeing an obvious error. The help is greatly appreciated.