1

I'm making a function that searches through a sentence to find a letter. Right now, I'm having trouble initializing a dynamic array for the user's sentence.

char * get_info(char *ch) {
  char *str;
  int i = 0;

  printf("Enter a sentence to seach: ");
  while ((str[i++] = getchar()) != '\n');
  str[i] = '\0';
  printf("Enter a character to search for: ");
  *ch = getchar();

return str;
}

The problem lies when I call str[i++]. Nothing I've tried or looked has helped so far. Any suggestions? Thanks!

7
  • 1
    So, what exactly happened when you called str[i++], and what did you expect to happen instead? Also, please post the remainder of the program. Commented Jun 13, 2014 at 2:37
  • @self. -- no, that is a different, more complicated situation (array of arrays). Commented Jun 13, 2014 at 2:38
  • 2
    You need to allocate memory for your char array. Commented Jun 13, 2014 at 2:38
  • 2
    Also also: you may be looking for getline. Commented Jun 13, 2014 at 2:38
  • @Zack The principle is the same, there are also dozens of existing questions that solve this. Find one that you think is adequate and mark as duplicate. Commented Jun 13, 2014 at 2:41

3 Answers 3

3

The problem is you are dereferencing an uninitialized variable. You need to allocate memory for str somehow.

Fortunately for you, there is a function that does this for you, called getline().

char * get_info(char *ch) {
  char *str;

  printf("Enter a sentence to seach: ");
  str = NULL;
  getline(&str, NULL, stdin);
  printf("Enter a character to search for: ");
  *ch = getchar();

  return str;
}
Sign up to request clarification or add additional context in comments.

3 Comments

... I should have seen that. +1 for you.
How is not guarding against a buffer overrun the fundamental problem when there isn't even a buffer allocated? Filling the input string using getchar() and dynamically using a larger buffer as needed essentially mitigates that problem.
@jxh I can see that, but that's because it isn't doing memory management at all. The OP is asking to dynamically read in the string, so it will need to be expanded at some point once the original buffer's limit is reached. getline() doesn't change that.
0

the declaration char* str, can be read as str is a variable that will hold a pointer to a character. No where in your code do you actually allocate this memory for a character (or an array of characters)

If you were to do the following:

char *str;
str = malloc(15*sizeof(char));

You have not only declared that str will contain a pointer to a character (or an array of characters), but also allocated this memory.

Now while this solves your original question, as jxh points out, this introduces another error. This code suffers from a classical heap-based buffer overflow, in that if I supply an input line with, say, 30 characters I will use all the memory allocated to the array and over-write other data.

Additionally, you do need to guard against memory allocation failure and attempt to gracefully handle it.

Comments

0

You could just implement something like this, if your goal is to find a particular letter within a sentence. It's certainly not perfect but should help you get an idea. Cheers!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){

    char sentence[30]; //store your sentence

    char letter[3]; //here we store the letter along with the EOL(\0)
    int i;


    puts("Please enter a sentence to search through: ");
    fgets(sentence, 30, stdin); //
    puts("Now enter the letter: ");
    scanf("%2s",letter); //max size for input is 2 including the EOL 


    if(strstr(sentence, letter)){

        printf("The letter '%s' can be found in the sentence.\n",letter);
    }

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.