0

I wrote a simple C program in Linux that reads a single character from a string. I get some error regarding string functions. This is my code:

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

void main () {
  char arr[10], vv[10];
  int i = 0, len;

  printf("enter the staement\n");
  scanf("%s", arr);
  len = strlen(arr);
  printf("String laength=%d\n", len);

  while ((vv[i] = getchar(arr)) != '\n') {
    printf("%d charcter\n");
    i++;
  }
}

I don't want to use getchar() directly on the input text like this:

arr[i] = getchar();

I want to use getchar() from a stored string like this:

getchar(string array);

But unfortunately I get an error. Can I use the getchar() function directly from a stored string array?

10
  • But arr is an array, so just use arr[x] where x is the character number you want to get? Commented Dec 19, 2014 at 14:11
  • There are also other problems with your code, like there not being a newline in the string you read with scanf, and many more. Commented Dec 19, 2014 at 14:14
  • @JoachimPileborg you dont need '\n' when you read a string! right? Commented Dec 19, 2014 at 14:16
  • Your last "prinf" dosen't make any sense. Commented Dec 19, 2014 at 14:17
  • This prinf("%d charcter\n"); has no integer argument passed to print! Commented Dec 19, 2014 at 14:17

3 Answers 3

2

Read about getchar. The link clearly says that getchar is a function that gets a character (an unsigned char) from stdin. Also, it takes no arguments. This would mean that you cannot copy each character of an array to another array using getchar. Just copy it directly using

while( (vv[i] = arr[i]) != '\n')

But I don't think this loop will end as scanf does not include the newline character when scanning a string(%s). So,you got two options:

  1. Use fgets to get input.
  2. Use the following

    while( (vv[i] = arr[i]) != '\0')
    
Sign up to request clarification or add additional context in comments.

Comments

1

When you have string in C, it is actually an array of chars which is terminated by '\0'. You do not need any method to get chars from it. Simply get the char as if you were accessing an array.

while((vv[i] = arr[i])!='\n')

As you have you arr[10] it will cause issues when your input is larger than 10 characters including the '\0'. So it is be better to declare it with enough space!

Comments

1

vv is a single char. You may not write vv[i].

Also, are you sure you want \n and not \0 [null]? scanf() won't give you a string with \n in it.

EDIT:

It is still unclear what you want to achieve, but if you want to check the presence of valid characters in the arr or vv, you can

  1. take the base address of the arr or vv into a char *p.
  2. check if (*p++) and do something.

EDIT:

You may try out something like

char * ip = NULL;
char * op = NULL;
int i = 10;              //same as array size.

ip = arr;
op = vv;

while( (*op++ = *ip++) && i--)
{

    //do something
};

2 Comments

sry , change to vv[10] array
@user39133 don't change the question in place. Now this answer does not make sense and I was going to downvote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.