Skip to main content
improving readability.
Source Link
Mark M
  • 2.3k
  • 2
  • 19
  • 26

I haven't used C in over 3 years, I'm pretty rusty on a lot of things.

I know this may seem stupid but I cannot return a string from a function at the moment. Please assume that: I cannot use string.h for this.

Here is my code:

#include <ncurses.h> char * getStr(int length) {  { char word[length]; for (int i = 0; i < length; i++)  { word[i] = getch(); } word[i] = '\0'; return word; } int main()   { char wordd[10]; initscr(); *wordd = getStr(10); printw("The string is:\n"); printw("%s\n",*wordd); getch(); endwin(); return 0; } 

I can capture the string (with my getStr function) but I cannot get it to display correctly (I get garbage).

Help is appreciated.

I haven't used C in over 3 years, I'm pretty rusty on a lot of things.

I know this may seem stupid but I cannot return a string from a function at the moment. Please assume that: I cannot use string.h for this.

Here is my code:

#include <ncurses.h> char * getStr(int length) {  char word[length]; for (int i = 0; i < length; i++)  { word[i] = getch(); } word[i] = '\0'; return word; } int main()  { char wordd[10]; initscr(); *wordd = getStr(10); printw("The string is:\n"); printw("%s\n",*wordd); getch(); endwin(); return 0; } 

I can capture the string (with my getStr function) but I cannot get it to display correctly (I get garbage).

Help is appreciated.

I haven't used C in over 3 years, I'm pretty rusty on a lot of things.

I know this may seem stupid but I cannot return a string from a function at the moment. Please assume that: I cannot use string.h for this.

Here is my code:

#include <ncurses.h> char * getStr(int length) { char word[length]; for (int i = 0; i < length; i++) { word[i] = getch(); } word[i] = '\0'; return word; } int main() { char wordd[10]; initscr(); *wordd = getStr(10); printw("The string is:\n"); printw("%s\n",*wordd); getch(); endwin(); return 0; } 

I can capture the string (with my getStr function) but I cannot get it to display correctly (I get garbage).

Help is appreciated.

clean up
Source Link
M.M
  • 142.7k
  • 26
  • 223
  • 400

I haven't used C in over 3 years, I'm pretty rusty on a lot of things.

I know this may seem stupid but I cannot return a string from a function at the moment. Please Please assume that: A) I cannot use string.hstring.h for this. B) I have to use ncurses.h and as few libraries as possible. C) I'm using gcc on a linux computer.

That said, hereHere is my code:

 #include <ncurses.h>  char * getStr(int length)  {   char word[length];  char *rtnPtr = word;   int i;  for (int i = 0; i < length; i++)   {   word[i] = getch();   }   word[i] = '\0';     return rtnPtr;word;  }  int main()  {   char wordd[10];   initscr();   *wordd = getStr(10);   printw("The string is:\n");   printw("%s\n",*wordd);   getch();   endwin();   return 0;  } 

I can capture the string (with my getStrgetStr function) but I cannot get it to display correctly (I get garbage).

Help is appreciated.

I haven't used C in over 3 years, I'm pretty rusty on a lot of things.

I know this may seem stupid but I cannot return a string from a function at the moment. Please assume that: A) I cannot use string.h for this. B) I have to use ncurses.h and as few libraries as possible. C) I'm using gcc on a linux computer.

That said, here is my code:

 #include <ncurses.h>  char * getStr(int length)  {   char word[length];  char *rtnPtr = word;   int i;  for (i = 0; i < length; i++)   {   word[i] = getch();   }   word[i] = '\0';     return rtnPtr;  }  int main()  {   char wordd[10];   initscr();   *wordd = getStr(10);   printw("The string is:\n");   printw("%s\n",*wordd);   getch();   endwin();   return 0;  } 

I can capture the string (with my getStr function) but I cannot get it to display correctly (I get garbage).

Help is appreciated.

I haven't used C in over 3 years, I'm pretty rusty on a lot of things.

I know this may seem stupid but I cannot return a string from a function at the moment. Please assume that: I cannot use string.h for this.

Here is my code:

#include <ncurses.h> char * getStr(int length) { char word[length]; for (int i = 0; i < length; i++) { word[i] = getch(); } word[i] = '\0'; return word; } int main() { char wordd[10]; initscr(); *wordd = getStr(10); printw("The string is:\n"); printw("%s\n",*wordd); getch(); endwin(); return 0; } 

I can capture the string (with my getStr function) but I cannot get it to display correctly (I get garbage).

Help is appreciated.

Source Link
MrWolf
  • 750
  • 1
  • 6
  • 6

Returning string from C function

I haven't used C in over 3 years, I'm pretty rusty on a lot of things.

I know this may seem stupid but I cannot return a string from a function at the moment. Please assume that: A) I cannot use string.h for this. B) I have to use ncurses.h and as few libraries as possible. C) I'm using gcc on a linux computer.

That said, here is my code:

 #include <ncurses.h> char * getStr(int length) { char word[length]; char *rtnPtr = word; int i; for (i = 0; i < length; i++) { word[i] = getch(); } word[i] = '\0'; return rtnPtr; } int main() { char wordd[10]; initscr(); *wordd = getStr(10); printw("The string is:\n"); printw("%s\n",*wordd); getch(); endwin(); return 0; } 

I can capture the string (with my getStr function) but I cannot get it to display correctly (I get garbage).

Help is appreciated.