Pointers Issue

This is a discussion on Pointers Issue within the C Programming forums, part of the General Programming Boards category; Hello, I am attempting to write a program in which I write my own version of the library function 'strcat' ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    22

    Pointers Issue

    Hello, I am attempting to write a program in which I write my own version of the library function 'strcat' using pointers:

    Code:
    #include "stdio.h"
    #include "string.h"
    
    void mystrcat(char *first, char *second);
    
    int main(void)
    {
        mystrcat("hello","world");
        getch();
        return 0;    
    }
        
    void mystrcat(char *first, char *second)
    {
        char result[strlen(first)+strlen(second)];   
        
        while(*first) *result++ = *first++;
        while(*second) *result++ = *second++;
        *result = '\0';
        printf(result);
    }
    When I try to compile it, I get two errors saying "wrong type argument to increment". What is the problem?

  2. #2
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    You're trying to increase dereferenced pointer to array, Eg increase the array.

    You could do *(&result[0])++; and it would be legal. But that's not what you want to do.

    You want to increase pointers, not memory content pointed by pointer.

    EDIT:

    Eg, not
    Code:
    while(*first) *result++ = *first++;
    while(*second) *result++ = *second++;
    but

    Code:
    while(*first) *(result++) = *(first++);
    while(*second) *(result++) = *(second++);
    EDIT2:
    I did not test this, but I assume that is the problem.
    Last edited by Maz; 08-30-2009 at 01:38 PM.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    22
    Thanks, I made those edits to my code but I get exactly the same error when I try to compile it.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, result is not a pointer, it is an array. You can't increment an array. Either use a separate int variable for an index that gets incremented, or set up a legitimate pointer variable to increment.

  5. #5
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    mmm... As I said, I have not tried my suggestion.

    Well, a dirtyish way could be:
    *( ((char *)result)++) = ...

    But you will propably get a compiler warning about casting the lvalue.

    Besides, if you plan to make your mystrcat() to return pointer to concatenated string, then having array allocated from stack and returning pointer to that will be a disaster. (When function is finished, concatenated string will be popped out of the stack, and pointer will point to released memory).

    I would suggest you to use malloc() instead.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    22
    Quote Originally Posted by tabstop View Post
    Well, result is not a pointer, it is an array. You can't increment an array. Either use a separate int variable for an index that gets incremented, or set up a legitimate pointer variable to increment.
    I thought you could use the name of an array without the index just like an ordinary pointer, but evidently this is not the case. I created another pointer like this

    Code:
    char *p;
    p = result;
    and used *p++ to increment it which compiles fine.

    However I still get a garbage result when I run the program. I assume this is because of what you just said Maz, but I am a beginner at C and I don't really know what you are talking about when you refer to these stacks.

  7. #7
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    103
    or just:
    Code:
    #include "stdio.h"
    #include "string.h"
    
    void mystrcat(char *first, char *second);
    
    int main(void)
    {
        
        mystrcat("hello","world");
        getch();
        return 0;    
    }
        
    void mystrcat(char *first, char *second)
    {
        char result[strlen(first)+strlen(second)];   
        int i;
        for(i=0; i < strlen(first) + strlen(second); i++) {
        result[i] = 0;        
        }
        
        char *pointer1 = &result[0];
        char *pointer2 = &first[0];
        char *pointer3 = &second[0];
        while(*pointer2) {
             *(pointer1++) = *(pointer2++);
        }
        
        while(*pointer3) {
            *(pointer1++) = *(pointer3++);
        }
        *pointer1 = '\0';
        printf(result);
    }

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    22
    Thanks Drogin, but I have corrected my code and it now works as it should:

    Code:
    #include "stdio.h"
    #include "string.h"
    
    void mystrcat(char *first, char *second);
    
    int main(void)
    {
        mystrcat("hello"," world");
        getch();
        return 0;    
    }
        
    void mystrcat(char *first, char *second)
    {
        char *p;
        char result[strlen(first)+strlen(second)];   
        
        p = result;
        
        while(*first) *p++ = *first++;
        while(*second) *p++ = *second++;
        *p = '\0';
        printf(result);
    }

  9. #9
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    103
    Ah, yeah, I see I overreacted about the pointers, lol

  10. #10
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    Your array is fine if you just print the value inside the strcat function. The problems come if you change your code to something along the lines:

    Code:
    char * mystrcat(char *first, char *second)
    {
        char result[strlen(first)+strlen(second)];   
        .
        .
        .
        return (char *) &result[0];
    }
    
    int main(void)
    {
      ...
        concatenated = mystrcat(str1,str2);
        /* later print concatenated or do something else with it */
        .
        .
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. moving pointers to pointers
    By Benzakhar in forum C++ Programming
    Replies: 9
    Last Post: 12-27-2003, 07:30 AM

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