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 11th, 2013, 02:54 PM
ele9 ele9 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 10 ele9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 39 m 22 sec
Reputation Power: 0
[C] convert array in string

Code:
#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>    

int main()  {        
    char *v=NULL ;          
    char val;          
    char size = 0;        
    int i;      
    do  {                
        printf ( "Inserire un nuovo elemento nell'array :");    
        scanf ("%c", &val);              
        v = (char*) realloc( v, (++size)*(sizeof(char)) );              
        v[size] = val;
    }  while (val != '0');      
    printf( "Elementi nell'array:");    
    for(i=0; i<size; i++){          
         if(v[i] <= '9' && v[i] >= '1' )  continue;          
         else  printf("%c ", v[i]);}      
    free(v);  
    return 0;
}. 

I wrote this code to digit in input a dinamic array with indefinite elements (numbers and letters) and in The end , print this array but only letters elements. The code works perfectly but now i would like to convert my array in a string but i have no idea how to do it. Could someone help me please??

Reply With Quote
  #2  
Old February 11th, 2013, 04:42 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,380 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 13 h 7 m 19 sec
Reputation Power: 383
Your program is not perfect. Each time you store a character into v you put it one position beyond the space allocated. Not only do you overwrite memory that is not yours, (certainly not v's), if reallocation moved the array rather than extending it you'd lose the character. Now, with slightly rewritten version of your program that displays the array, I give it the input
2340
the output is
^@234
You didn't store a character at v[0].
Code:
#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>    

int main()  {        
    char *v=NULL ;          
    char val;          
    int size = 0;        
    int i;      
    do  {                
        printf ( "Inserire un nuovo elemento nell'array :");    
        scanf ("%c", &val);              
        v = (char*) realloc( v, (++size)*(sizeof(char)) );              
        v[size] = val;
    }  while (val != '0');      
    printf( "Elementi nell'array:");    
    for(i=0; i<size; i++)
      putchar(v[i]);
    putchar('\n');
    free(v);
    return 0;
}
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old February 11th, 2013, 05:39 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,136 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 3 Days 21 h 10 m 31 sec
Reputation Power: 1974
Quote:
Originally Posted by ele9
The code works perfectly but now i would like to convert my array in a string but i have no idea how to do it.

All you need to do is to add one more character to the end of the char array you've created, the null-terminator, which is '\0' . A null-terminated char array is a C-style string.

Well, you also need to make the correction that was pointed out to you.

Reply With Quote
  #4  
Old February 11th, 2013, 06:25 PM
ele9 ele9 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 10 ele9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 39 m 22 sec
Reputation Power: 0
first of all Thanks to booth for your answer. I posted The wrong code ( i'm id**t) and now i tested it also with input 1240 and it should be ( i hope!) ok. About The NULL terminator string , could i use a cicle like this(?):

Code:
    if ( v[i] == '\n' )
    {
        V[i] = '\0';
        break;
    }
}








Code:


#include <stdio.h>
#include <stdlib.h>

int main()  {
      char *v ; 
      char val; 
      int size = 0;
      int i;



do  { 

            printf ( "Inserire un nuovo elemento nell'array :");


scanf ("%c", &val);
            v = (char*) realloc( v, (++size)*(sizeof(int)) );
            v[size-1] = val;  

      } while (val != '0'); 


printf( "Elementi nell'array:"); // stampa normalmente l'intero array nominato v[]


 for(i=0; i<size-1; i++){
        if(v[i] <= '9' && v[i] >= '1' )  continue;
        else  printf("%c ", v[i]);}


free(v);
return 0; }




Reply With Quote
  #5  
Old February 11th, 2013, 07:31 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,380 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 13 h 7 m 19 sec
Reputation Power: 383
Your new version is good, except that the NULL initial value
char *v = NULL;
in your original is critical. This version has a few other changes including changing the final '0' to nul making an ASCIIz string.
Code:
#include <stdio.h>
#include <stdlib.h>

#define ORIGINAL 1 /* setting ORIGINAL to 0 blocks digits from the string */

int main()  {
  char *v = NULL;		/* = NULL   //IMPORTANT */
  char val;
  int size = 0;
  int i;
  do  {
    printf ( "Inserire un nuovo elemento nell'array :");
    scanf ("%c", &val);

    /* don't admit unwanted characters */
    if (ORIGINAL || (val <= '0') || ('9' < val)) {
      v = (char*)realloc(v,(++size)*(sizeof(int))); /* sizeof char was large enough */
      if (NULL == v) {	     /* handle the potential memory problem */
	exit(1);
      }
      v[size-1] = val;
    }
  } while (val != '0');
  printf( "Elementi nell'array:"); // stampa normalmente l'intero array nominato v[]

  for(i=0; i<size-1; i++){
    if(v[i] <= '9' && v[i] >= '1' )  continue;
    else  printf("%c ", v[i]);
  }

  putchar('\n');
  v[size-1] = 0;		/* overwrite the '0' with ASCII nul */
  printf("As nul terminated string <%s>.\n",v);

  free(v);
  return 0;
}

Last edited by b49P23TIvg : February 11th, 2013 at 07:37 PM.

Reply With Quote
  #6  
Old February 11th, 2013, 07:38 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,136 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 3 Days 21 h 10 m 31 sec
Reputation Power: 1974
No, you're exiting the loop before you get to the '\n', so that makes no sense.

Rather, after you leave the do-while loop, you need:
Code:
            v = (char*) realloc( v, (++size)*(sizeof(int)) );
            v[size-1] = '\0';


And you also need to initialize that pointer, v, before you make the first call to realloc. I thought we discussed that before!

Here is what the documentation says about realloc (http://www.manpagez.com/man/3/realloc/):
Quote:
The realloc() function tries to change the size of the allocation pointed
to by ptr to size, and returns ptr. If there is not enough room to
enlarge the memory allocation pointed to by ptr, realloc() creates a new
allocation, copies as much of the old data pointed to by ptr as will fit
to the new allocation, frees the old allocation, and returns a pointer to
the allocated memory. If ptr is NULL, realloc() is identical to a call
to malloc() for size bytes. If size is zero and ptr is not NULL, a new,
minimum sized object is allocated and the original object is freed. When
extending a region allocated with calloc(3), realloc(3) does not guarantee
that the additional memory is also zero-filled.

You should initialize v as
char *v = NULL;
Read the documentation on realloc to understand why.

Reply With Quote
  #7  
Old February 11th, 2013, 08:44 PM
ele9 ele9 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 10 ele9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 39 m 22 sec
Reputation Power: 0
Thanks Thanks Thanks !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Finally my code works correctly!!!!!!!!!!!!!!!!!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > [C] convert array in string

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