Thread: problem with pointers

  1. #1
    Registered User
    Join Date
    May 2013
    Location
    Mauritius
    Posts
    4

    problem with pointers

    Hi, i am new to c programming. i still learning the basics. i have been trying writing simple code to get use to the commands. however i am stuck on a problem that i cannot solve. the codes are based on string manipulation.

    there are 2 problems i am facing:
    1)
    Code:
    #include<stdio.h>
    
     #include<string.h>
    
     int main(void)
     {
    
         char *str1 = "cprogramming";
    
         char *str2 = "this is new";
    
          printf("\n\nstr1 : %s\t\tstr2 : %s\n\n",str1,str2);
    
          printf("\n\nstrncpy(str1,str2,5): %",strncpy(str1,str2,5));
    
         getch();
      }
    the program is suppose to replace the s first characters of str1 with the first 5 character of str2, the problem is each time 1 run the code, the window stops responding. However when i use
    Code:
     
    char str1[80]= "c programming";
    char str2[80]= "this is new";
    it works..

    2)
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void main (void)
    {
        char *fullname, *firstname, *surname;
        printf("Enter Full Name (Surname first): ");
        gets(fullname);
        firstname = strstr(fullname," ");
        strnset(firstname,"",1);
        strncpy (surname,fullname,(strlen(fullname)-(strlen(firstname)+1)));
        printf("\n\nOutput: %s %s",firstname, surname);
        getchar();
    }
    Here the program was suppose to search for the space between two words entered and stores the word before the space into surname and the word after the space to firstname. but here also the windows stops responding when i run the code.

    can someone help me figured where i did something wrong.

    thanks.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    In your first example you're trying to modify string literals. You can't they are read only.
    In the second example you write into uninitialiized pointers. You have to allocate space using malloc().
    Kurt

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    As a side note - avoid the use of 'gets' and use 'fgets' when possible.

    Code:
    fgets(firstname, sizeof(firstname), stdin);
    firstname[strlen(first name) - 1] = 0;
    Last edited by swgh; 05-07-2013 at 02:58 AM. Reason: Used wrong slash on edit
    Double Helix STL

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by swgh
    Code:
    firstname[strlen(first name) - 1] = 0;
    That is not correct since the last character might not be the newline character to be discarded. A similiarly concise yet correct alternative:
    Code:
    firstname[strcspn(firstname, "\n")] = '\0';
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2013
    Location
    Mauritius
    Posts
    4
    Quote Originally Posted by laserlight View Post
    That is not correct since the last character might not be the newline character to be discarded. A similiarly concise yet correct alternative:
    Code:
    firstname[strcspn(firstname, "\n")] = '\0';
    thanks a lot. but as i said i'm still on the basics. i think this was too much for,i didn't knew the existence of the command strcspn and fgets. i will go through more documentation on string manipulation and will try to write the program again!!!

  6. #6
    Registered User
    Join Date
    May 2013
    Location
    Mauritius
    Posts
    4
    ok, i tried to write the program again and well, i'm stuck with another problem. when i enter a name with the firstname and the surname of the same length the codes work fine but the problem comes when the length of the surname and the firstname differs. the codes works till the end but there are some additional characters with the firstname and the program stops reponding. i can't figure why.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    /***the purpose of this program is to search for the space between the two word entered, set
    firstname to the word before the space and set surname to the word entered after the space***/
    
    void main ()
    {
        char *fullname=malloc(sizeof(fullname)),*firstname=malloc(sizeof(firstname)),*surname=malloc(sizeof(surname));
        int lengthfullname,lengthfirstname,lengthsurname;
        printf("Enter Full Name: ");
        gets(fullname);
        if (fullname == NULL) {
            printf("\n\nout of memory\n\n");
        }
        else {
            surname = strstr(fullname," ");
            lengthfullname  = strlen(fullname);
            lengthsurname = strlen(surname);
            lengthfirstname = (lengthfullname - lengthsurname);
            firstname = strncpy(firstname,fullname,lengthfirstname);
            surname = strcpy(surname,(surname+1));
            printf("\n\nOutput: %s %s\n\n",surname, firstname);
            free (firstname),(surname),(fullname);
        }
    }

  7. #7
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    You're still using gets(), don't. Use fgets().

    main always returns int, so instead of void main() you should write int main(). Also, your calls to malloc only allocates sizeof(char *), which probably isn't what you want. Instead of using dynamic allocation for this, use statically allocated char arrays:

    Code:
    char fullname[64], firstname[32], surname[32];
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  8. #8
    Registered User
    Join Date
    May 2013
    Location
    Mauritius
    Posts
    4
    Quote Originally Posted by Neo1 View Post
    You're still using gets(), don't. Use fgets().

    main always returns int, so instead of void main() you should write int main(). Also, your calls to malloc only allocates sizeof(char *), which probably isn't what you want. Instead of using dynamic allocation for this, use statically allocated char arrays:

    Code:
    char fullname[64], firstname[32], surname[32];
    i have tried static allocation before, it gives error: incompatible types when assinging to types 'char[32]' from type 'char *' while using the string commands like strstr() or strncopy() and the program don't complie..

    and also thnx i change the gets() to fgets()

  9. #9
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by tarounen View Post
    i have tried static allocation before, it gives error: incompatible types when assinging to types 'char[32]' from type 'char *' while using the string commands like strstr() or strncopy() and the program don't complie..

    and also thnx i change the gets() to fgets()
    Try your best, and if you still can't get it to work post the code here and we can help with the errors you're getting. Using malloc() for this isn't necessary. If you still insist on using malloc(), you need to allocate enough room for the entire string, remember that sizeof(char) returns the size of ONE char, you'll need more than that.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with pointers?
    By hiimjoey11 in forum C Programming
    Replies: 32
    Last Post: 10-23-2012, 03:10 AM
  2. Problem with Pointers
    By DeliriumCordia in forum C Programming
    Replies: 19
    Last Post: 03-27-2012, 04:28 PM
  3. Problem with malloc and pointers to pointers
    By mike_g in forum C Programming
    Replies: 7
    Last Post: 03-29-2008, 06:03 PM
  4. A problem with pointers
    By vsla in forum C Programming
    Replies: 2
    Last Post: 10-10-2007, 04:14 AM
  5. problem with pointers
    By dionys in forum C Programming
    Replies: 2
    Last Post: 05-27-2004, 06:13 PM

Tags for this Thread