Thread: Function result as a string

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    16

    Function result as a string

    Hi, here I have a small function, that checks if the string doesn't exceed 10 characters - now I'm stuck with a problem - pointer... How should I tell that the value of the function is variable "nos" - I tried different variants, but it seems that I won't get more than just errors

    Code:
    char *string3 (char *s){
    int n;
    char *nos;
    
    n=1;
    
    printf("%s: ",s);
    
    while (((scanf("%s",&nos)) != 1) || ((strlen(nos))>10))
      {
       clrscr();
       while (getchar()!='\n');
        printf ("Error nr. %d. Try again.\n%s: ",n,s);
        n+=1;
      }
    }

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    What you said you were doing, and what you did aren't the same. What are you really attempting to do?

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    16
    Sorry, if I didn't explain you the problem correctly - what would you like to know? I must say, that the code works fine, I just don't understand how to pass variable nos to function string3 at the end - return ...?

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Code:
    char *nos;
    
    n=1;
    
    printf("%s: ",s);
    
    while (((scanf("%s",&nos)) != 1) || ((strlen(nos))>10))
    There is NO WAY this can work fine. nos is pointing to NOTHING, yet you are dereferencing that nothingness to get a string from scanf. THIS IS BROKEN CODE. IT CANNOT WORK, PERIOD. So, as I stated before, what you said you were doing and what you did aren't the same.

    Please CLEARLY state the problem and post _ALL_ of your code if you want help.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    16
    Sorry, just tried it, and I must say you're right - it doesn't work. It seems that I've mistyped something while posting this topic, unfortunately I haven't saved the previous version :/ I'll try to rewrite it.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    100
    Well.. the point (at least top begin with) is you never allocate any space for 'nos'.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by nowber View Post
    Hi, here I have a small function, that checks if the string doesn't exceed 10 characters - now I'm stuck with a problem - pointer... How should I tell that the value of the function is variable "nos" - I tried different variants, but it seems that I won't get more than just errors

    Code:
    char *string3 (char *s){
    int n;
    char *nos;
    
    n=1;
    
    printf("%s: ",s);
    
    while (((scanf("%s",&nos)) != 1) || ((strlen(nos))>10))
      {
       clrscr();
       while (getchar()!='\n');
        printf ("Error nr. %d. Try again.\n%s: ",n,s);
        n+=1;
      }
    }
    There are so many problems in the code
    first of all what do you want to do ??? first major question from me

    and

    this function signature says it should return something of char* which is not returning any thing error

    in the while loop what you want to do is out of my mind

    you are incrementing n and you are making no use of it

    you are scanning nos prinint s

    and the biggest thing is segmentation fault with this code

    and the major problem with you type of guys is you won't compile the code at your end and you just post the code here

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Replies: 6
    Last Post: 04-21-2006, 08:49 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM