Thread: copying a string

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    1

    Exclamation copying a string

    here is my code
    Code:
    #include <stdio.h>
    
    #define MAX 81
    
    
    
    void substring(char *n,int i,int j)
    {int k;
    char substr[MAX];
       for(k=0;k<=j;k++)
       {
       printf("%c",*(n+i+k));
       *substr=*(n+k+i);
       substr++;
    
    
    }
    *substr='\0';
    printf("%s",substr);
    }
    int main(void)
    {
    
        char name[MAX];  /* allot space                  */
    
    
    
        printf("Hi, what's your name?\n");
    
        gets(name);      /* place string into name array */
    
        printf("Nice name, %s.\n", name);
    
    substring(name,2,1);
    
    
        return 0;
    
    }
    its giving an error "error: wrong type argument to increment"

    i think i am doing right but still getting error .please someone suggest something

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    substr is an array, not a pointer.
    You need to write substr[k] to progress through the array.

    Also, read the FAQ on not using gets(), and also pay attention to indentation.
    Code:
    #include <stdio.h>
    
    #define MAX 81
    
    void substring(char *n, int i, int j)
    {
      int k;
      char substr[MAX];
      for (k = 0; k <= j; k++) {
        printf("%c", *(n + i + k));
        *substr = *(n + k + i);
        substr++;
      }
      *substr = '\0';
      printf("%s", substr);
    }
    
    int main(void)
    {
      char name[MAX];               /* allot space                  */
    
      printf("Hi, what's your name?\n");
      gets(name);                   /* place string into name array */
      printf("Nice name, %s.\n", name);
      substring(name, 2, 1);
    
      return 0;
    }
    Finally, use meaningful names in your function declaration, like
    void substring(char *name, int startpos, int length)
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-10-2011, 05:25 AM
  2. Copying a string, into a string array.
    By m.mixon in forum C Programming
    Replies: 5
    Last Post: 07-31-2006, 05:19 PM
  3. String copying
    By bobthebullet990 in forum C Programming
    Replies: 12
    Last Post: 11-28-2005, 09:31 AM
  4. Replies: 1
    Last Post: 10-31-2005, 11:36 AM
  5. Copying one string from s2[] to s1[]
    By steve_demaio in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 05:01 PM

Tags for this Thread