Thread: "cannot convert parameter 1 from 'const char * []' to 'char *'" error

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    25

    a high school programming question and an issue about an error

    Hi,

    I've some problems to get over, first of all, i keep getting this error and couldn't get it solved. here is the code and the error i get:

    the error: error C2664: 'strtok' : cannot convert parameter 1 from 'const char []' to 'char *'

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    void countletters(const char a[]);
    
    int main() {
    	char string[80];
    	puts("Text Analysis 1.0");
    	puts("Please enter a string to be analyzed");
    	scanf("%s",string);
    	countletters(string);
    	return 0;
    }
    
    void countletters(const char a[]) {
    	int letters[26]={0};
    	int length;
    	int wordlength[11];
    	char *token;
    	int i,x;
    	length = (int)strlen(a);
    	printf("Lenght of the text is: %d\n",length);
    	puts("Number of occurences of each letter:");
    	for (i=0; i<length; ++i) {
    		x=tolower(a[i]);
    		x -= 97;
    		++letters[x];
    	}
    	for (i=0;i<26;++i) {
    		printf("%c:%d\t",97+i,letters[i]);
    		if (!((i+2)%9)) printf("\n");
    	}
    	puts("The lengths of the words in the sentence:");
    	token = strtok(a," ");
    	while (token != NULL) {
    		x=strlen(token);
    		++wordlength[x];
    		strtok(NULL, " ");
    	}
    	for (i=1;i<11;++i)
    		printf("%d%s%d\n",i," letter words: ",wordlength[i]);
    }
    Second question: This piece of code is from a high school programming contest, the question is to determine the output by mind. I answered the question wrong, give it a try and please explain the output comes out you chose(by the way, don't mind the preprocessor statements, return 0, etc):

    Code:
    void f(int p[],int q[])
    {
    	int a[5]={1,2,3,4,5};
    	static int b[5]={-1,-2,-3,-4,-5};
    	p = a;
    	q = b;
    }
    main()
    {
    	int i,j, p[5]={2,4,6,8,10}, q[5]={1,3,5,7,9};
    	f(p,q);
    	for(i=0;i<5;i++) p[i] += q[i];
    	for(i=0;i<5;i++) printf("%d ",p[i]);
    }
    a) 1 2 3 4 5
    b) 2 5 8 11 14
    c) 0 1 2 3 4
    d) 3 7 11 15 19
    e) none of the above

    Thank you for your help and kind concern!
    Last edited by kolistivra; 07-06-2006 at 12:25 PM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    the error: error C2664: 'strtok' : cannot convert parameter 1 from 'const char []' to 'char *'
    strtok wants to modify the char array you pass, so it cannot be const.
    Kurt

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    For the second question it looks like d is the winner to me. The f() function has no effect on the main() function. Not sure why j is there either. Of course, d isn't quite right either because the printf() doesn't have a space in the format string so the numbers would come out in one long string: 37111519

    If there truly isn't a space in the printf() format string I would have to go with e instead.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    25
    well, you are right itsme86, the correct answer is d(btw, i forgot to put space there, there is one)

    what i couldn't get is that, you say the function has no effect on main, but

    Code:
    p = a;
    q = b;
    shouldn't these change the values of the first elements of the arrays?

    for ZuK:
    I have modified the code and removed conts, now there is no error, but my code seems to not be working(the segment "The lengths of the words in the sentence"), where am i mistaken?

    Thanks in advance

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    the array wordlength is not initialized.
    and don't try to analyze sentences containing words longer then 10 chars
    Kurt

  6. #6
    Registered User
    Join Date
    Jul 2006
    Posts
    25
    Thank you ZuK, it seems to be working fine now, with your suggestion as well as another bug i have found

    Thank you again.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    what i couldn't get is that, you say the function has no effect on main, but


    Code:
    p = a;
    q = b;

    shouldn't these change the values of the first elements of the arrays?
    Nope. f() is only setting its local variables to those arrays. It's the same as:
    Code:
    void f(int a)
    {
      a = 5;
    }
    int main(void)
    {
      int a = 3;
      f(a);
      printf("%d\n", a);
      return 0;
    }
    That program will still print 3. If you wanted to change the value of a in main() you'd have to pass a pointer to a instead of the value of a. The same goes with your mind question. You'd have to pass a pointer to the pointer if you want to change the pointer in main().
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Jul 2006
    Posts
    25
    Ok, but isn't the issue a bit different for arrays? AFAIK, arrays are passed by reference, unlike other data types. So, shouldn't their actual values change? If you can clarify this point a little bit, I would be appreciative,

    Thank you in advance

  9. #9
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by kolistivra
    AFAIK, arrays are passed by reference, unlike other data types.
    Kind of. Copies of pointers to the first element in the array are passed. Your code just excanges this pointers. the values are not touched.
    Kurt
    EDIT:
    if you want to see an output of all zeros, you have to code f() like this

    Code:
    void f(int p[],int q[]) {
            int i;
    	int a[5]={1,2,3,4,5};
    	static int b[5]={-1,-2,-3,-4,-5};
            
            for ( i = 0; i < 5; ++i )
    	    p[i] = a[i];
            for ( i = 0; i < 5; ++i )
    	    q[i] = b[i];
    }
    Last edited by ZuK; 07-07-2006 at 11:18 AM.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    AFAIK, arrays are passed by reference, unlike other data types.
    An array decays to a pointer to its first element when it is passed as an argument, and this pointer is passed by value.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM