Thread: Treating Character Arrays as Strings

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    8

    Treating Character Arrays as Strings

    Hello!

    I am writing some code for multiple purposes. I've already compiled the parts with the numeric arrays. The issues that I've been having are dealing with stringSpace(char name).

    Here is my code:

    Code:
    #include <stdio.h>#include <stddef.h>
    #include <stdlib.h>
    
    
    void printArray(int n[]);
    void randomPop(int array2[]);
    void stringSpace(char name);
    
    
    int main(void)
    {
        int i;
        int n[4];
        int array2[20];
        char name[8] = "Serenity";
        
        for (i = 0; i < 4; ++i) {
            printf("Enter value %d. \n", i+1);
            scanf("%d", &n[i]);
        }
       
        printf("%s%13s\n", "Element", "User's Value");
        printArray(n);
        
        printf("%s%13s\n", "Element", "Random Value");
        randomPop(array2);
        
        printf("%s", "Name with spaces: \n");
        stringSpace(name);
        
        return 0;
    }
    void printArray(int n[])
    {
        int i;
        for (size_t i = 0; i < 4; ++i)  {
            printf("%7zu%13d\n", i, n[i]);
        }
    }
    void randomPop(int array2[])
    {
        int j;
        for (size_t j = 0; j < 20; ++j) {
            array2[j] = rand() % 100 + 1;
            printf("%7zu%13d\n", j, array2[j]);
        }
    }
    void stringSpace(char name)
    {
        char m;
        for (size_t m = 0; m < SIZE && name[m] != '\0'; ++m)    {
            printf("%c", name[m]);
    }
    I'm not quite sure how to go about this. I've been trying to follow an example from my C textbook, but it isn't leading me in the right place. In the example, the user is prompted for the characters, while I already have the characters, "Serenity," that I want to be printed.

    Overall, I'm trying to print out the word, "Serenity," but with spaces between each letter: "S e r e n i t y."

    I want to establish a prototype for this and call it from main.

    Any help would be appreciated. The errors that I'm facing for the most part mostly include:
    -note: expected ‘char’ but argument is of type ‘char *’
    -error: subscripted value is neither array nor pointer nor vector

    "Name" must be being read as a pointer, if I'm not mistaken. Is there a way to fix this? I'm trying to figure out how to initialize a character array but as a string.

    Thanks for all help in advance. I really appreciate it.

  2. #2
    Registered User
    Join Date
    Apr 2019
    Posts
    62
    Code:
    char name[8] = "Serenity";
    This is wrong. This allocates an array exactly 8 characters wide and initializes it with the 8 characters from the string but doesn't copy the 0 byte at the end to mark the end of the string. Instead do one of these.

    Code:
    char name[] = "Serenity";
    char *name = "Serenity";
    As for stringSpace, the parameter needs to be of type char *, not char. You also have some conflicting variables here, you have a char m that you're not using and immediately shadowing by size_t m. I also don't see SIZE in the program. You're also not printing out any spaces, just the characters from the string itself. I'd shorten up that syntax a bunch and just do this.

    Code:
    void stringSpace(char *s) {
      for(; *s; s++) printf("%c ", *s);
    }
    But using array subscripting, like this.

    Code:
    void stringSpace(char *s) {
      for(int i = 0; s[i]; i++) printf("%c ", s[i]);
    }
    Both functions are the same, which one you use is a matter of taste.

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    8
    Thank you so much for your help!
    Everything is working greatly now and I have a better grasp of the concept.

    Best regards!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character Arrays and Strings?
    By ocswimfast in forum C Programming
    Replies: 4
    Last Post: 09-15-2010, 10:44 AM
  2. copying strings to heap character arrays
    By SkyRaign in forum C++ Programming
    Replies: 4
    Last Post: 11-26-2006, 02:08 PM
  3. Adding character arrays to strings.
    By w00tw00tkab00t in forum C++ Programming
    Replies: 28
    Last Post: 02-06-2006, 07:03 PM
  4. gibrish when copying strings character by character
    By captain-cat in forum C Programming
    Replies: 2
    Last Post: 07-23-2004, 07:48 AM
  5. strings or character arrays
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 07-21-2002, 10:55 AM

Tags for this Thread