0

I'm making a simple 'game' with ncurses in C, and for name input I use getnstr. I have the following code:

printw("What is your name? ");
char name[10];
int namelen = getnstr(name, 10);

Now, I want to allow max 10 characters, and I want to use the length of the name for the border columns. However, namelen is 0 for some reason (I thought getnstr returned the length).

How can I get the correct length of the name? So if I insert Josh as name I get 4. sizeof(name) returns 10 so it's no use.

Joshua Bakker
  • 2,288
  • 3
  • 30
  • 63
  • You want to read in your C book how to get the length of a string and what the `sizeof` operator is for. – too honest for this site Nov 29 '16 at 12:03
  • @Olaf 'my C book' I don't have a C book. I didn't think I could use `strlen` on a char array but only on a string. About `sizeof`, I expected it to not work but I just mentioned it. – Joshua Bakker Nov 29 '16 at 12:04
  • "I don't have a C book" - So what do you conclude? It would tell you what `sizeof` is and what a C "string" is. Hint: C does not have a string type! – too honest for this site Nov 29 '16 at 12:06
  • @Olaf I wanna say before I start I'm a 19 year old student and I'm doing C and C++ in my free time, I don't work much since I'm busy with school mostly so I don't get much income. I know what `sizeof` is. I just didn't think of C not having a `string` type (of course not as it doesn't have types). – Joshua Bakker Nov 29 '16 at 12:10
  • "I know what sizeof is." - apparently not, otherwise it would not have you sirprised (resp. you had not used it to get the length of the "string". Not sure what the rest is supposed to say. There are free books and you likely also buy books for school. Let that apart, you did not show any re**search** effort. How to get the length of a string has been asked and answered along on SO dozens of times already. – too honest for this site Nov 29 '16 at 12:22
  • @Olaf No I never said I was surprised by that - don't put words in my mouth. I don't buy books for school since the past 2 years I barely used a book. I have a Java book but I didn't even use it. – Joshua Bakker Nov 29 '16 at 12:28

2 Answers2

3

Why not do this :

char s[10];
scanf("%10s", s);
printf("Length : %d\n", (int) strlen(s));

For strlen, do #include <string.h> first.

Jarvis
  • 8,494
  • 3
  • 27
  • 58
  • Why do you consider heap allocation to be 'accordingly'? Stack allocation is cheaper and easier to manage. – V. Kravchenko Nov 29 '16 at 11:38
  • @V.Kravchenko By 'accordingly', I meant the OP can change the size of allocation, like `sizeof(char)*x`, OP can choose `x`. – Jarvis Nov 29 '16 at 11:39
  • Even so, code is error-prone, as it misses free statement. Why you replaced original char array declaration? – V. Kravchenko Nov 29 '16 at 11:41
  • `scanf` doesn't work too well with ncurses, still thanks for the help anyways. Accepted the other guy's answer because he also gave some explanation about the `getnstr` function. – Joshua Bakker Nov 29 '16 at 11:47
  • @Jarvis That additional opportunity to change size of allocation is not needed here. Features that are not needed are waste of time. He could as well write a wrapper struct for char pointer and array size, write additional functions for that string management, but it is not needed in this very concrete task. – V. Kravchenko Nov 29 '16 at 11:57
  • @Jarvis sure thing, already did that :) Every answer that helps me - or tried to help me - gets an upvote. – Joshua Bakker Nov 29 '16 at 12:01
2

Accordly to the documentation, getnstr doesn't return the length of the string but OK or ERR (http://pubs.opengroup.org/onlinepubs/007908799/xcurses/getnstr.html).

If you need the length of the string, use strlen (string.h).

PS : if you want to allow max 10 char, you need an array of 11 char (10 char + \0)

EDIT : Found on curses.h code :

#define ERR (-1)            /* Error return. */
#define OK  (0)         /* Success return. */
Fefux
  • 964
  • 5
  • 12
  • No, I didn't mean that, I'm asking what exactly do you want, other than my suggestion ? @JoshuaBakker – Jarvis Nov 29 '16 at 11:40
  • @JoshuaBakker I trust you, it works maybe in this case. But if you write 10 char, you're null character is on the 11th array position and this position doesn't exist (segmentation fault) – Fefux Nov 29 '16 at 11:41
  • May I know, what's wrong with my answer ? (since you didn't accept it :( ) @JoshuaBakker – Jarvis Nov 29 '16 at 11:46
  • Thanks for helping, this really helped me out; thanks for the link and the defines too! @Jarvis nothing, read my comment on your answer. – Joshua Bakker Nov 29 '16 at 11:48