LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-20-2012, 11:18 AM   #1
doughyi8u
Member
 
Registered: Apr 2010
Posts: 136

Rep: Reputation: 1
problem printing pointer


I have the following code:
Code:
#include <stdio.h>
#include <stdlib.h>

char *my_strcpy(char *dest, char *source);

int main(int argc, char *argv[])
{
  char ptr1[] = "this is a string";
  char ptr2[80], *ret;
  ret = my_strcpy(ptr2, ptr1);
  /*puts(ret);*/
  puts(ptr2);
  system("PAUSE");	
  return 0;
}

char *my_strcpy(char *dest, char *source)
{
  int i = 0;
  while (*source) {
        *(dest++) = *(source++);
  }       
    *(dest) = '\0';
    puts(dest);    /*this doesn't print*/
    return dest;
}
The line in question has a comment in red. It Won't print but it does print ptr2 in main. I'm confused.
 
Old 12-20-2012, 11:35 AM   #2
orgcandman
Member
 
Registered: May 2002
Location: dracut MA
Distribution: Ubuntu; PNE-LE; LFS (no book)
Posts: 594

Rep: Reputation: 102Reputation: 102
On the line in question, the value of dest is not the same as the value of ptr2.

Additionally, the value of ret, in the other commented puts will not be the same as ptr2.

Think about it a bit - you've been incrementing dest - where does it point on the line in question?
 
1 members found this post helpful.
Old 12-20-2012, 11:53 AM   #3
doughyi8u
Member
 
Registered: Apr 2010
Posts: 136

Original Poster
Rep: Reputation: 1
How do I return dest and make it work? I tried passing it like so:
Code:
return &dest[0];
But that didn't work.
I also tried return *(dest - (strlen(source)));
but that didn't work either.
 
Old 12-20-2012, 02:21 PM   #4
orgcandman
Member
 
Registered: May 2002
Location: dracut MA
Distribution: Ubuntu; PNE-LE; LFS (no book)
Posts: 594

Rep: Reputation: 102Reputation: 102
You need to think about what your variables hold a bit more. You actually don't need to declare anything new, but need to make use of something you're not.

I suggest another glance through the K&R C.
 
Old 12-21-2012, 06:20 AM   #5
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora, Arch
Posts: 572

Rep: Reputation: 64
Code:
char *my_strcpy(char *dest, char *source)
{
    char *original_dest = dest;
    while (*source) {
        *dest++ = *source++;
    }       
    *dest = '\0';
    puts(original_dest);
    return original_dest;
}
I also removed some superfluous parentheses.

Last edited by KenJackson; 12-21-2012 at 06:23 AM.
 
Old 12-26-2012, 10:20 AM   #6
orgcandman
Member
 
Registered: May 2002
Location: dracut MA
Distribution: Ubuntu; PNE-LE; LFS (no book)
Posts: 594

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by KenJackson View Post
Code:
char *my_strcpy(char *dest, char *source)
{
    int i = 0;
    while (*source) {
        *dest++ = *source++; ++i;
    }       
    *dest = '\0';
    puts(dest - i);
    return dest - i;
}
Emphasis on the red lines, for a way of writing the implementation without declaring a new variable from the code written. NOTE: I support Ken's approach. Just wanted to make sure the OP understood my earlier comment.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer problem in c. kalleanka Programming 5 01-28-2009 04:58 AM
returning data to main() via a pointer to a pointer. slzckboy Programming 3 05-30-2005 01:20 PM
pointer dereference problem cranium2004 Programming 4 05-03-2005 09:34 AM
hot to set value of pointer to pointer to a structure in C alix123 Programming 2 11-17-2004 06:40 AM
Problem about pointer in C akin81 Programming 5 04-03-2004 06:44 AM


All times are GMT -5. The time now is 09:26 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration