Вы находитесь на странице: 1из 2

#include <stdio.

h>
#include <stdlib.h>
#include<string.h>

typedef struct
{
char *artist = NULL;
char *title = NULL;
} songInfo;

void getSongInfo(songInfo *arr, char artist[], char string[]);


void printSongInfo(songInfo **arr);

int main(void)
{
//dynamically creating array of size 10 using malloc
songInfo *arr = (songInfo*)malloc(10 * sizeof(songInfo));
if (arr == NULL)
{
return 1;
}

for (int i = 0; i < 10; i++)


{
printf("Please Enter Song Info %d :", i + 1);
getSongInfo(&arr[i], arr->artist, arr->title);
}
//printSongInfo(arr);
// testing to print out
for (int i = 0; i < 10; i++)
{
printf("%s", arr[i].artist);
printf("%s", arr[i].title);

//printf("%-40s %-40s\n", arr[i].artist, arr[i].title);


}
}
void getSongInfo(songInfo *arr, char artist[], char string[])
{
char input[30] = { '\0' };
char artistName[30] = { '\0' };
char titleName[30] = { '\0' };

scanf("%s%s", artistName, titleName);

//memory allocation for array


(arr) = (songInfo*)malloc(10 * sizeof(songInfo));

//memory allocation for artitst


arr->artist = (char*)malloc((strlen(artistName) + 1) * sizeof(char));

//memory allocation for title


arr->title = (char*)malloc((strlen(titleName) + 1) * sizeof(char));

strcpy((arr)->artist, artistName);

strcpy(arr->title, titleName);
//arr->artist = artistName;
//arr->title = titleName;

puts(arr->artist);//testing
puts(arr->title);
//There are no words to express how I feel about you, I'll just say Thank you
for everything & I LOVE YOU "MAA". HAPPY MOTHER'S DAY

//void printSongInfo(songInfo **arr)


//{
// for (int i = 0; i < 10; i++)
// {
// printf("%-40s %-40s\n",arr[i]->artist,arr[i]->title);
//
// }
//}

Вам также может понравиться