Like Tree1Likes
  • 1 Post By MK27

Need help with pointer problem

This is a discussion on Need help with pointer problem within the C Programming forums, part of the General Programming Boards category; I am studying C my self and trying to do some exercise. The exercise requires 2 functions, one to takes ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    5

    Exclamation Need help with pointer problem

    I am studying C my self and trying to do some exercise.
    The exercise requires 2 functions, one to takes three parameters (one string holds questions, one string holds user 's answers, and one integer limits the characters of the answer. The other function is the main function that uses the previous function to ask the user and prints the full name.
    I also tried to write the code but there is a problem which I can not find it out by myself. Can any one show me the mistake and explain it for me ?
    Thanks all.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    #define SIZE 100
    
    void func(char, char, int);
    
    int main(void)
    {
        char str1[] = "Enter your first name: ";
        char str2[] = "Enter your middle name: ";
        char str3[] = "Enter your last name: ";
        char answer[SIZE];
        
        func(str1, answer, SIZE);
        
        func(str2, answer, SIZE);
        
        func(str3, answer, SIZE);
        
        printf("Your full name is: %s", answer);
    }
    
    void func(char *a, char *b, int max_size)
    {
        char temp[100];
        int i;
        
        printf("%s", a);
        gets(temp);
    
        i = strlen(temp);
                
        if(i < max_size)
        {
            strcat(b, temp);
        }
        else
        {
            printf("You enter too much characters.");
        }
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    First, stop using gets()
    SourceForge.net: Gets - cpwiki

    Next, make sure answer is initialised to be an empty string before you start trying to strcat() into it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    5
    Quote Originally Posted by Salem View Post
    First, stop using gets()
    SourceForge.net: Gets - cpwiki

    Next, make sure answer is initialised to be an empty string before you start trying to strcat() into it.
    But if I empty the string answer, how can I join the three parts of the name?

    When I tried to run the solution, it appeared like this:

    Code:
    Enter your first name: suyaku // I entered suyaku
    Enter your middle name: kudo // I entered kudo
    Enter your lastname: suyakukudo // I did not entered anything but this "suyakukudo" appeared, and if I press "Enter" button, an error box appeared... "stack arround str2 is corrupted"
    Last edited by suyaku92; 03-12-2012 at 12:41 PM.

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    As Salem says, you should empty the answer variable *first* before you try to join the three parts of the name. Otherwise you may have garbage values in there.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    Quote Originally Posted by suyaku92 View Post
    But if I empty the string answer, how can I join the three parts of the name?

    When I tried to run the solution, it appeared like this:

    Code:
    Enter your first name: suyaku // I entered suyaku
    Enter your middle name: kudo // I entered kudo
    Enter your last name: suyakukudo // I did not entered anything but this "suyakukudo" appeared, and if I press "Enter" button, an error box appeared... "stack arround str2 is corrupted"
    Use strcat. Just make sure you put a space between the names.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Post your latest code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    5
    Quote Originally Posted by Salem View Post
    Post your latest code.
    The code I posted first time is the lastest !

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > The code I posted first time is the lastest !
    But I gave you two things to fix, and it still looks the same.

    So either you've done nothing, except to complain that it still doesn't work. If this is what you're saying, then you're just wasting everyone's time.

    Or you've made some changes, and it still doesn't work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    5
    Quote Originally Posted by Salem View Post
    > The code I posted first time is the lastest !
    But I gave you two things to fix, and it still looks the same.

    So either you've done nothing, except to complain that it still doesn't work. If this is what you're saying, then you're just wasting everyone's time.

    Or you've made some changes, and it still doesn't work.
    first, the result I posted after run the solution is NOT CHANGED after I changed gets to scanf (follow your 1st instruction)
    second, i do not know how to follow 2nd instruction so i asked you again but i haven't got the answer yet

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by suyaku92 View Post
    The code I posted first time is the lastest !
    Quote Originally Posted by suyaku92 View Post
    I changed gets to scanf (follow your 1st instruction)
    So let's see it. But first...

    second, i do not know how to follow 2nd instruction so i asked you again but i haven't got the answer yet
    But if I empty the string answer, how can I join the three parts of the name?
    QuantumPete answered this in post #4, but either you did not read it, or you did not understand.

    This:
    Code:
    char answer[SIZE];
    could contain any kind of garbage, because it has not been initialized. "Initialize it first" and "making sure it is empty" means something like this:

    Code:
    char answer[SIZE] = { 0 };
    This will zero out the array. That's important, because string functions look for the first zero to, eg, concatenate onto.
    suyaku92 likes this.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Mar 2012
    Posts
    5
    Quote Originally Posted by MK27 View Post
    So let's see it. But first...





    QuantumPete answered this in post #4, but either you did not read it, or you did not understand.

    This:
    Code:
    char answer[SIZE];
    could contain any kind of garbage, because it has not been initialized. "Initialize it first" and "making sure it is empty" means something like this:

    Code:
    char answer[SIZE] = { 0 };
    This will zero out the array. That's important, because string functions look for the first zero to, eg, concatenate onto.
    Thanks all, it works.
    I did not understand ( I told you I just study C myself and I did not encounter this problem before).
    Sorry for your time.
    Problem solved.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    2,351
    Everyone seems to have failed to spot the following mistake:

    the function is declared as follows:
    Code:
    void func(char, char, int);
    but then it is defined like so:
    Code:
    void func(char *a, char *b, int max_size)
    this should be throwing compiler errors (or at least warnings) related to incorrect parameter types. perhaps you need to turn up the warning level on your compiler.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with pointer to a pointer variable
    By Rodri in forum C Programming
    Replies: 2
    Last Post: 11-20-2011, 09:50 AM
  2. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  3. sturct/pointer problem, and fscanf problem
    By hiphop4reel in forum C Programming
    Replies: 6
    Last Post: 07-28-2008, 09:40 AM
  4. Replies: 4
    Last Post: 11-05-2006, 01:57 PM
  5. pointer to pointer how do i solve following problem?
    By kobra_swe in forum C Programming
    Replies: 5
    Last Post: 07-19-2006, 04:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21