•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 426,198 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,878 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 881 | Replies: 6
![]() |
•
•
Join Date: Mar 2007
Posts: 8
Reputation:
Rep Power: 0
Solved Threads: 1
Question:I ran into a point where it would be very helpful to send a function a portion of an array, instead of the entire thing. Now i can think of a couple work arounds like sending the whole array and a begin number, and range number, then computing off of that, or making a second array with only the portion needed and sending it.
I am hoping however that there is a simpler way.
int myfunc(array[]) {}
x = myfunc(array[from a to b]);
is essentially the idea though I am certain its not in syntax.
Reason: In my programming a calculator I am ending up with some what is fairly convoluted code to essentially make it take any equation entered, compute it and return its output. And I would like to save 8 lines of code and hopefully a few steps for the processor.
as in:
string equation = 3+(42*-3.46);
answer = value_of(equation);
where value_of() is an absurdly complex function hehe
I am hoping however that there is a simpler way.
int myfunc(array[]) {}
x = myfunc(array[from a to b]);
is essentially the idea though I am certain its not in syntax.
Reason: In my programming a calculator I am ending up with some what is fairly convoluted code to essentially make it take any equation entered, compute it and return its output. And I would like to save 8 lines of code and hopefully a few steps for the processor.
as in:
string equation = 3+(42*-3.46);
answer = value_of(equation);
where value_of() is an absurdly complex function hehe
•
•
Join Date: Feb 2007
Location: Bangalore, India
Posts: 535
Reputation:
Rep Power: 4
Solved Threads: 50
Given you're using std::string you have many easy alternatives:
1. Use
basic_string substr(size_type pos = 0, size_type n = npos) const;
Returns a string composed of copies of the lesser of n and size() characters in this string starting at index pos.
Throws an out_of_range exception if pos > size().
2. Use this constructor which creates new string object using a substring of an existing strnig object: basic_string (const basic_string& str, size_type pos, size_type n= npos, const allocator& a=allocator());
If you're really concerned abt teh performance, you might wann use char* instead of std::string. In this case if you want to pass a sub string, the begin part can be taken care of by pointer arithmatic. The end part is tricky and it would be best (in terms of performance) if you pass another integer to indicate the end.
E.g.
1. Use
basic_string substr(size_type pos = 0, size_type n = npos) const;
Returns a string composed of copies of the lesser of n and size() characters in this string starting at index pos.
Throws an out_of_range exception if pos > size().
2. Use this constructor which creates new string object using a substring of an existing strnig object: basic_string (const basic_string& str, size_type pos, size_type n= npos, const allocator& a=allocator());
If you're really concerned abt teh performance, you might wann use char* instead of std::string. In this case if you want to pass a sub string, the begin part can be taken care of by pointer arithmatic. The end part is tricky and it would be best (in terms of performance) if you pass another integer to indicate the end.
E.g.
c Syntax (Toggle Plain Text)
void comlex_func( char* equetion, const int length ) ; int main() { char equation[] = "1+2+3*4" ; //say we want to pass "2+3" part to function. comlex_func( equation + 3, 3 ) ; return 0; }
•
•
Join Date: Feb 2007
Location: Bangalore, India
Posts: 535
Reputation:
Rep Power: 4
Solved Threads: 50
•
•
•
•
c Syntax (Toggle Plain Text)
void comlex_func( char* equetion, const int length ) ; int main() { char equation[] = "1+2+3*4" ; //say we want to pass "2+3" part to function. comlex_func( equation + 3, 3 ) ; return 0; }
Just a warning...
c Syntax (Toggle Plain Text)
void comlex_func( char* equetion, const int length ) ; int main() { char equation[] = "1+2+3*4" ; //say we want to pass "2+3" part to function. comlex_func( equation + 3, 3 ) ; return 0; } void comlex_func( char* equetion, const int length ) { printf("What I got is: ") ; for( int i = 0; i < length; i++ ) printf( "%c", equetion[i] ) ; printf("Beware the null char is not at teh end of supplied length: %s", equetion ) ; }
Last edited by thekashyap : Mar 24th, 2007 at 7:27 am.
•
•
Join Date: Feb 2007
Location: Bangalore, India
Posts: 535
Reputation:
Rep Power: 4
Solved Threads: 50
>Just wondering what problem do you see.. ?
I don't see a problem thus far. You've gotta split the array into tokens. I was just saying without careful planning for every possible outcome it is likely to fail. His/her design at the mo looks very simplistic, almost as if she/he's doing as she goes.
I don't see a problem thus far. You've gotta split the array into tokens. I was just saying without careful planning for every possible outcome it is likely to fail. His/her design at the mo looks very simplistic, almost as if she/he's doing as she goes.
Last edited by iamthwee : Mar 24th, 2007 at 7:40 am.
I'm not a programmer. My attitude starts with ignorance, holds steady at conversation, and ends with a trip to the hospital. Get used to it.
•
•
•
•
Just a warning...printf("Beware the null char is not at teh end of supplied length: %s", equetion ) ; }
•
•
•
•
Reason: In my programming a calculator I am ending up with some what is fairly convoluted code to essentially make it take any equation entered, compute it and return its output. And I would like to save 8 lines of code and hopefully a few steps for the processor.
This is a very bad reason to do anything. Assess what something easier means. Does it make the code harder to read? Do you have to resort to tricks to accomplish your goal? Will these tricks compromise maintainability (making it harder to change the code later)? Just trying to save a couple nano-seconds of processor time is not a good thing. Readability should not be compromised.
Age is unimportant -- except in cheese
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Converting a String into an integer array (C)
- On Click Event for an array of buttons (Java)
- Help with a button array... (Java)
Other Threads in the C Forum
- Previous Thread: Returning An Array
- Next Thread: Set combinations function



Linear Mode