Function return Array

Status
Not open for further replies.

Help

Advanced Member level 2
Joined
Feb 15, 2005
Messages
617
Helped
7
Reputation
14
Reaction score
3
Trophy points
1,298
Activity points
7,065
Hi,

I have this function
Code:
char code * Array(void)
{
 ....
 num[5] = "12345";
 return num;
}

void copystry( const char *ary)
{
  char buff[5]=0;

  buff = ary; // How can i copy the array num[]="12345" to buff?
}

void Func(void)
{
   copystrg(Array());
}

There is some error on the coding. Anyone please can you help me?

Thank you.
 

You can use library function strcpy.
Code:
#include <string.h>


void copystry(const char *ary) 
{ 
  char buff[5]; 

  strcpy(buff, ary);
}


You cant return buff as its a local variable and will go out of scope!
If you dont want to use library function.




Code:
for(ix = 0; ix < SIZEOFBUFF; ix++)
{
buff[ix] = *ary++;
}
 

    Help

    Points: 2
    Helpful Answer Positive Rating
here is an example of Function that could get data from other function... hope this would help...

the program would allow the user to delete a number from the displayed array...

#include<stdio.h>
main()
{
int del(int *a, int value, int *size);
static int array[] = {11,12,13,14,15,16,17,18,19,20};
int num, i=10, c, found;
char exit;

do
{
clrscr();
printf("Original Array of Numbers\n");
for(c = 0; c < i ; ++c)
printf("%3d", array[c]);

printf("\n\nEnter a data to be deleted: ");
scanf("%d", &num);

c = i;
found = del(array, num, &i);

if(found == 1)
{
printf("\n%d is deleted from the array\n", num);
for(c=0; c<i; ++c)
printf("%3d", array[c]);
}
else
printf("Data not found");

printf("\nDo you want to delete another number(Y/N)?");
exit = getch();

}while(exit != 'n' && exit !='N');

}

int del(int *a, int value, int *size)
{
int i, c;
for(i = 0; i<*size; ++i)
{
if(a==value)
{
for(c = i; c<=*size-1; ++c)
a[c] = a[c+1];

*size = c-1;
return(1);
}
}
}
 

    Help

    Points: 2
    Helpful Answer Positive Rating
From K&R:

Code:
/* strcpy: copy t to s; subscript version */
void strcpy(char *s, char *t) 
{
    int i;
    i=0;
    while ((s[i] = t[i] != '\0')
        i++;
}

or
Code:
/* strcpy: copy t to s; pointer version 1 */
void strcpy(char *s, char *t) 
{
    while((*s = *t) != '\0'){
          s++;
          t++;
    }
}

or
Code:
/* strcpy: copy t to s; pointer version 2 */
void strcpy(char *s, char *t) 
{
    while((*s++ = *t++) != '\0') ;
}

Hope this Helps,

Red
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…