Pointer Error

This is a discussion on Pointer Error within the C Programming forums, part of the General Programming Boards category; Hey guys, I'm fairly new to C programming and don't understand pointers very well. This program compares two strings and ...

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

    Pointer Error

    Hey guys, I'm fairly new to C programming and don't understand pointers very well. This program compares two strings and outputs the suffix (suffix of "globally" and "internally" is "ally"). I am getting this error: suffix.c:21:2: warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast [enabled by default]
    It also seg faults if I run the program. What is my problem?

    Code:
    #include <stdio.h>
    #include <string.h>
    int main()
    {
            char first[80];
            char second[80];
            printf("Please enter the first word: ");
            fgets(first, 80, stdin);
            printf("Please enter the second word: ");
            fgets(second, 80 ,stdin);
            int i;
            int different = 0;
            for(i=1; different == 0; i++)        {       
                    if (first[strlen(first)-i] != second[strlen(second)-i])
                    different = 1;
            }
            char same[80];
            strcpy(same, first[strlen(first)-i]);
            printf("%s", same);
            return 0;
    }//main

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    Ok a function from a library makes your code to have an error.Why?Because you pass wrong parameters.Your friend, the compiler, tells you which ones you pass wrong.Now you pass wrong the second one.

    So how to solve this?Take a look at reference of strcpy

    Then think about what this code does
    Code:
    int array[10]; /* Declare an array of ints */
    array[4] = 5; /* Set the fifth element of the array to 5 */
    So when i say array[number] i access only one element of the array.

    Then think about what strlen returns to you...Hmmm... A number!Practical an unsigned int.

    So when you say
    Code:
    int length = strlen(first)-i;
    you will get variable length equal to the length of string first(without the terminating null ) minus the value of i.

    So when you write this
    Code:
    first[strlen(first)-i]
    it is equivalent to write it like this
    Code:
    int length = strlen(first)-i
    first[length];
    This will give access in one letter of the string , not the whole string.If did not cheat and look at the reference, you will understand that strcpy receives as arguments strings and not characters.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    Ok I see. So I am only giving it one character when it needs a whole string. So how would I give the second half of the string? Can I use strcpy? Or is there another function that will copy only the last bit of characters to a new string?

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    Here you have what you need

  5. #5
    Dweeb dojha00's Avatar
    Join Date
    Feb 2012
    Location
    Global
    Posts
    23
    What i understand is that u want to store second half of the string in 'same' string using strcpy function and u want to send the address of second half---
    if it's so then write
    Code:
    length=strlen(first);
    strcpy(same,(first + length - i));
    What is life??

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    Wow that was super simple thanks

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Quote Originally Posted by dojha00 View Post
    What i understand is that u want to store second half of the string in 'same' string using strcpy function and u want to send the address of second half---
    if it's so then write
    Code:
    length=strlen(first);
    strcpy(same,(first + length - i));
    Another way to write that is like this:
    Code:
    length = strlen(first);
    strcpy(same, &first[length - i]);
    which I personally find to be a little clearer than just adding a pointer with indexes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer Error
    By abhishekcoder in forum C Programming
    Replies: 4
    Last Post: 02-08-2012, 02:44 PM
  2. Replies: 4
    Last Post: 10-31-2009, 07:18 PM
  3. Pointer error.
    By george_1988 in forum C Programming
    Replies: 9
    Last Post: 09-06-2008, 12:47 AM
  4. Pointer error
    By Gaming in forum C++ Programming
    Replies: 8
    Last Post: 05-03-2008, 03:55 AM
  5. Pointer(s) Error
    By gsoft in forum C Programming
    Replies: 4
    Last Post: 11-25-2004, 05:25 AM

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