C - Problem passing array as pointer

This is actually in C, but I can't seem to find any C forums that are nearly as good as the forum here. I'm writing a program that accepts a string of input and then converts the letters to "sticky case" (alternating upper and lower case). My sticky function must accept a pointer to the string in order to alter it globally. My compiler is giving a warning when I pass "string" into sticky(), even though it should be a valid pointer to the beginning of the array. When I run the program, it crashes after input is entered, with the message "STATUS_ACCESS_VIOLATION". Please help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <stdlib.h>

/*converts ch to upper case, assuming it is in lower case currently*/
char toUpperCase(char ch){
    return ch-'a'+'A';
}

/*converts ch to lower case, assuming it is in upper case currently*/
char toLowerCase(char ch){
    return ch-'A'+'a';
}

void sticky(char* string[]){
    /*Convert to sticky caps*/
    int i;
    while(*string[i] != '\0'){
    	if(i % 2 == 0){
    		toLowerCase(*string[i]);
    	}else{
    		toUpperCase(*string[i]);
    	}
    	i++;
    }
}

int main(){
	char string[50];

    /*Read word from the keyboard using scanf*/
    printf("Please enter the longest word you can think of: ");
    scanf("%s", string);
    printf("%s", string);

    /*Call sticky*/
    sticky(string);
    
    /*Print the new word*/
    printf("That's \"%s\" in sticky text!\n", string);

    return 0;
}
Illegal type conversion. The sticky function accepts an array of char pointers. You are passing just a pointer,not an array of them. And the variable i is not initialized in the sticky function.
Thanks! I've made some changes and everything compiles without warnings, but it still crashes at the same point. Here is my main:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main(){
    char* string[50];

    /*Read word from the keyboard using scanf*/
    printf("Please enter the longest word you can think of: ");
    scanf("%s", *string);
    printf("%s", *string);

    /*Call sticky*/
    sticky(string);
    
    /*Print the new word*/
    printf("That's \"%s\" in sticky text!\n", *string);

    return 0;
}
Last edited on
Your problem was not in main() it was with sticky(), so why did you change main()? What you have now created in main() is a pointer to an array of char and you never initialize that pointer. The pointer is not requiired.

As a hint the prototype for sticky() should have been:
1
2
3
void sticky(char string[]);
// or
void sticky(char *string);
I see. Thanks, I think some of my confusion is because this started as a skeleton program that I needed to fill in, and the parameter for sticky was written as char* string[], so I've been trying to make that work somehow. I've changed it and it seems to work!
Topic archived. No new replies allowed.