Thread: Pointers stuff

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    2

    Post Pointers stuff

    getting trouble with this program.
    trying to return a reverse string to main but it doesn't work.
    when I put the function into main it works.
    I think I'm not using the correct way dealing with pointers.
    What am I doing wrong?

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    char * rev(char * word);
    int main()
    {
           char word1[100];
           char rev1[100];
           printf("Enter a word");
           scanf("\n%s",word1);
           strcpy(rev1,rev(word1));
           printf("\nThe reverse order of this word is: %s",rev1);
           return 0;
    
    
    
    
    }
    char * rev(char * word)
    {
        int len=strlen(word);
    
    
        char reverse[len];
        int j=0;
        for(int i=len-1;i>=0;i--)
        {
               reverse[i]=word[j];
               ++j;
        }
        reverse[len]='\0';
        return reverse;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    There are two (or 3) things wrong with your rev function.
    1. Your local reverse array is too short (there is no room for a \0)
    2. Your local array goes out of scope when you return. You don't return arrays, you return only pointers.
    3. Only C99 supports variable length arrays.

    Assuming you want to keep the original string, you need something like this.
    Code:
    char *rev(const char *word, char *reverse) {
      // your existing logic
      return reverse;
    }
    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.

  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    150
    Shouldn't this post be in the C section? It's not C++.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Quote Originally Posted by thmm View Post
    Shouldn't this post be in the C section? It's not C++.
    Yes, moved.
    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.

  5. #5
    Registered User
    Join Date
    May 2021
    Posts
    2
    Quote Originally Posted by Salem View Post
    There are two (or 3) things wrong with your rev function.
    1. Your local reverse array is too short (there is no room for a \0)
    2. Your local array goes out of scope when you return. You don't return arrays, you return only pointers.
    3. Only C99 supports variable length arrays.

    Assuming you want to keep the original string, you need something like this.
    Code:
    char *rev(const char *word, char *reverse) {
      // your existing logic
      return reverse;
    }
    thank u for helping -now it works

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CRC stuff
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 02-07-2010, 11:19 AM
  2. pointers with strings and stuff.
    By DLH112 in forum C Programming
    Replies: 9
    Last Post: 10-21-2009, 04:51 PM
  3. Replies: 23
    Last Post: 05-11-2008, 07:12 AM
  4. Some stuff I did.
    By taelmx in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 11-05-2006, 01:26 PM
  5. kinda wierd stuff with pointers...
    By Noobwaker in forum C Programming
    Replies: 4
    Last Post: 04-17-2006, 02:21 PM

Tags for this Thread