Passing a pointer to a function?

I would like to write a function that sums the int value of char* data type.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string> 
using namespace std;
int main(int argc, const char * argv[])
{
     char* string = "Hello World!";
    const int size = strlen(string);
    int sum = 0; 
    cout << string << endl; 
    for (int i = 0; i < size; i++) {

        sum += *string++;;
        
    }
    cout << sum << endl; 
    return 0;
}



above is working version of how I think I can implement it.
Something tells me I am overlooking rules about passing char pointers to functions

I am thinking that the specification of my function should look like this..
 
int charToInt(char *input, size)


any help would be appreciated.
Last edited on
If your string is null terminated, you do not need to pass a size wit it. If a null symbol is valid inside string, you have to pass size too.
Topic archived. No new replies allowed.