1

I am trying to copy name array to another array and print it

#include <stdio.h>

typedef struct 
{
    char name[100];
    int age;
} data; 

int main() {
    char new_array[100];
    data people[] = {{ "john", 12},{" kate", 15}};
    for(int i =0; i < sizeof(people); i++) {
        new_array[i] = people[i].name;
        printf("%c ", new_array[i]);
    }

    return 0;
}

But it gives me an error:

error: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Werror=int-conversion]
     new_array[i] = people[i].name;
                  ^

How do I fix this?

3
  • 2
    strcpy and What is a String in C may be handy. This has more to do with copying a string (strcpy/strncpy) than copying array contents (memcpy). Also, a semantic name like person_name may be more helpful than new_array. Commented May 5, 2020 at 20:28
  • "The C Programming Language" can also be a nice introduction to many basics - it includes many walk-through examples. This (or a similar) book is often assigned for courses. Commented May 5, 2020 at 20:35
  • 2
    C11 Standard - 6.3.2.1 Other Operands - Lvalues, arrays, and function designators(p3) "... an expression that has type "array of type" is converted to an expression with type "pointer to type" that points to the initial element of the array object and is not an lvalue. Commented May 5, 2020 at 20:35

2 Answers 2

2

You can change:

char new_array[100];

to:

char new_array[10][100]; // for maximum 10 strings

Then using strcpy to copy string in c. If you want to calculate the number of elements of array, using:

sizeof(people)/sizeof(people[0]);

Then, the for loop becomes:

for(int i =0; i < sizeof(people)/sizeof(people[0]); i++) {
        strcpy(new_array[i],people[i].name);
        printf("%s ", new_array[i]);
}
Sign up to request clarification or add additional context in comments.

4 Comments

you can also using char ** pointer, then allocate it using malloc if you do not want to use array..
it's better to use strncpy instead of strcpy
why strncpy is better ?
strcpy is fine if you can guarantee the source is NULL-terminated and fits within the destination.
2

You are trying to assign a string to a char which is described by your error. To copy a string to a char array, you should use strcpy().

Also, your new_array is a mere array of characters and has no way to differentiate two different names.

To make it an array of strings, you should use a 2D array where you can index the row to get different strings like below

char new_array[10][100]

This makes an array of 10 strings of 100 characters each.

Also, your iteration over the array of structure is messy.

To get correct size of array of structure, you should use

int size = sizeof(people)/sizeof(people[0])

So, your final code becomes-

#include <stdio.h>
#include <string.h>

typedef struct 
{
    char name[100];
    int age;
} data; 

int main() {
    char new_array[10][100];
    data people[] = {{ "john", 12},{" kate", 15}};
    for(int i =0; i < sizeof(people)/sizeof(people[0]); i++) {
        strcpy(new_array[i],people[i].name);
        printf("%s ", new_array[i]);
    }

    return 0;
}

5 Comments

trying to assign a string literal to a char it's not problem
new_char[i] is a single character having size = sizeof(char), "john" is a string literal with size = 5*sizeof(char), 4 for each character j,o,h,n and '\0'. String literals are of type char *. Assigning char * to a single character is the problem here. How can you assign 4 characters to a single character? This is the problem. Read String literal here learn.microsoft.com/en-us/cpp/c-language/…. Correct me if I am wrong.
when you declare in struct char name[100], it is an array of characters. So you can modify the value in this array. It becomes problem if in struct, the OP use char * name
Aaghh!! Missed that declaration and gone with the initialization... I will update in my answer as well.
OT, new_array[10][20] should be new_array[10][100] because the size of name in struct is 100.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.