C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 15th, 2013, 12:18 PM
Uniflame Uniflame is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 6 Uniflame User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 32 m 49 sec
Reputation Power: 0
C string problem.

Hello. I am actually a student and I just started creating my own little programms but I have a problem. I tried to create a program which was supposed to reverse the word test(to tset). I managed to compile it but when I try to print the result nothing comes up. its empty. Heres the code.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *convert(char *a);
int main()
{
char a[5]="Test";
char *p;
p=convert(a);
printf("%s", p);
free(p);
system("pause");
}

char *convert(char *a)
{
char *array;
int i;
array=(char *)malloc(5*sizeof(char));
for (i=0;i<5;i++)
{

array[i]=a[5-i-1];
}
array[5]='\0';
return array;
}

Reply With Quote
  #2  
Old February 15th, 2013, 12:41 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,256 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 20 h 36 m 23 sec
Reputation Power: 1985
Use code tags to post code here. That preserves your code's formatting and keeps it readable.

Code:
char *convert(char *a)
{
    char *array;
    int i;
    array=(char *)malloc(5*sizeof(char));
    for (i=0;i<5;i++);
    {
        array[i]=a[5-i-1];
    }
    array[5]='\0';
    return array;
}

Analyze array:
array[0] = 't'
array[1] = 's'
array[2] = 'e'
array[3] = 't'
array[4] = '\0'
array[5] is outside the bounds of the 5-element array and so "does not exist", but in reality by writing to this location you are clobbering (overwriting) whatever other data or function control value is stored there. The Thing and The Hulk can clobber, but you must not. Therefore, array[5]='\0'; is just plain wrong and must be corrected. But that's not the problem you wrote to us about.

This statement,
array[i]=a[5-i-1];
is clumsy and stylistically would be better rendered as
array[i]=a[4-i];
Actually, that a index would in a general solution be [strlen(a)=i]; in this case strlen(a) would be 4. The for-loop should also iterate for strlen(a) -- personally, I would have assigned that function's return value to a variable for reuse.
But that's not the problem that you wrote to us about.

Look at that for-loop. I tried to bold the semicolon at the end, but you still might not see it. What you are telling the compiler is that there is block of code attached to this for-statement, so it loops 5 times doing notion except to iterate the value of i, then, since i == 5, it clobbers whatever is there with whatever comes right before the array a (ie, what's at a[-1]) and moves on. As a result, the array you return is filled with garbage, since you've never actually written to it.

Getting rid of that semicolon should clear up the problem you wrote to us about, but you have some other problems to clean up as well.

Reply With Quote
  #3  
Old February 15th, 2013, 12:55 PM
Uniflame Uniflame is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 6 Uniflame User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 32 m 49 sec
Reputation Power: 0
Quote:
Originally Posted by dwise1_aol
Use code tags to post code here. That preserves your code's formatting and keeps it readable.

Code:
char *convert(char *a)
{
    char *array;
    int i;
    array=(char *)malloc(5*sizeof(char));
    for (i=0;i<5;i++);
    {
        array[i]=a[5-i-1];
    }
    array[5]='\0';
    return array;
}

Analyze array:
array[0] = 't'
array[1] = 's'
array[2] = 'e'
array[3] = 't'
array[4] = '\0'
array[5] is outside the bounds of the 5-element array and so "does not exist", but in reality by writing to this location you are clobbering (overwriting) whatever other data or function control value is stored there. The Thing and The Hulk can clobber, but you must not. Therefore, array[5]='\0'; is just plain wrong and must be corrected. But that's not the problem you wrote to us about.

This statement,
array[i]=a[5-i-1];
is clumsy and stylistically would be better rendered as
array[i]=a[4-i];
Actually, that a index would in a general solution be [strlen(a)=i]; in this case strlen(a) would be 4. The for-loop should also iterate for strlen(a) -- personally, I would have assigned that function's return value to a variable for reuse.
But that's not the problem that you wrote to us about.

Look at that for-loop. I tried to bold the semicolon at the end, but you still might not see it. What you are telling the compiler is that there is block of code attached to this for-statement, so it loops 5 times doing notion except to iterate the value of i, then, since i == 5, it clobbers whatever is there with whatever comes right before the array a (ie, what's at a[-1]) and moves on. As a result, the array you return is filled with garbage, since you've never actually written to it.

Getting rid of that semicolon should clear up the problem you wrote to us about, but you have some other problems to clean up as well.


Thank you for the quick answer I managed to fix the problem.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > C string problem.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap