Pointer to C string?

For int input in CS, I'm writing a C ++ program in Visual Studio 2010 that returns an integer and takes a C string pointer as an argument. I know I need to make a function other than int main to succeed, but I'm not really sure how to initialize an array of char pointers that points to a predefined array of char, if possible.

The purpose of this program is to get a comment from a user within a predefined limit and then tell the specified user how many characters (including spaces) the comment is.

Error: value of type "char" cannot be assigned to entity of type "char *";

Here is my program that doesn't compile:

#include "stdafx.h"
 #include <iostream>
 #include <string>
 #include <conio.h>
 using namespace std;

 //function protoype
 void evalStr(char arr[]);
//variable
int length;


//main function
int main()
{
    const int SIZE = 201;
    char arr[SIZE];
    char *str[SIZE];
    cout << "Please enter a comment under " << (SIZE - 1) << " characters to calculate it length: ";
    *str = arr[SIZE];

    cin.getline(arr, SIZE);


    length = strlen(*str);
    evalStr(arr);

    system("PAUSE");
    return 0;
}
//function defintion
/*  get the string
    count the number of characters in the string
    return that number

*/
void evalStr(char arr[])
{
    printf("The length of the entered comment is %d characters\n", length);
}

      

If there is a common method of using arrays of char pointers, or perhaps pointers to strings, this code can be modified to return the value of the string instead of using this printf statement. What am I doing wrong?

Edit: Here is an updated version of this program that compiles, launches and informs the user if the character limit is reached or exceeded.

// Accept a pointer to a C-string as an argument 
// Utilize the length of C-string in a function. 
// Return the value of the length
// Display that value in a cout statement.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;


//variables
const int SIZE = 201;
int length;

char arr[SIZE];
char *str;


//main function
int main()
{
    str = &arr[0];

    // Size - 1 to leave room for the NULL character
    cout << "Please enter a comment under " << (SIZE - 1) << " characters to calculate it length: ";


    cin.getline(arr, SIZE);


    length = strlen(str);
    if (length == (SIZE - 1))
    {
        cout << "Your statement has reached or exceeded the maximum value of " 
             << length << " characters long.\n";
    }
    else
    {
    cout << "Your statement is ";
    cout << length << " characters long.\n";
    }


    system("PAUSE");
    return 0;
}
//function defintion
/*  get the string
    count the number of characters in the string
    return that number

*/
int countChars(int)
{
    length = strlen(str);

    return length;
}

      

+3


source to share


2 answers


Let's talk about what's going on main

instead:

int main()
{

      

Ok, so you have 201 character strings. Seems reasonable.

    const int SIZE = 201;

      

And you declared a 201 character array:

    char arr[SIZE];

      

And now you are declaring an array of 201 character pointers. I'm not sure why you would like to do this. I suspect you are thinking this is doing something different from what it actually does:

    char *str[SIZE];

      

This makes sense (other than "this" means "this", but you want a possessive version of "her"):



    cout << "Please enter a comment under " << (SIZE - 1) << " characters to calculate it length: ";

      

This assigns the 201st character to the first character pointer in your char array of char pointers. This is an error because:

  • arrays are null-indexed, so the 201st character (when you start counting from zero) is outside the array.
  • you have not yet initialized memory in arr

    .
  • you assign char

    for char *

    .

So, given the above, I'm not sure why you are doing this:

    *str = arr[SIZE];

      

This looks reasonable:

    cin.getline(arr, SIZE);

      

This is an error because it str

does not point to memory containing a valid string at that point.

    length = strlen(*str);

      

+3


source


When you "point to a string", you are actually pointing to the first character of the string. The end of the string can be found by looking through consecutive memory locations until you find a location that contains a null byte. Therefore, your code should be:



char *str;         // can point at a character
// ....
str = &arr[0];    // point to the first character of arr
// ....
length = strlen(str);

      

0


source







All Articles