1

I need your help! Why in C structures of data that stores in char strings works only 1 type of declaration: char *name; works, but char []name; does not work.

But when to try declare a char string inside the code (without using struct), everything works. Code example that illustrate, what when to declare char array, both of the declaration types works.

#include "funct.h"
#include "stdio.h"

//structure employee name and surname only works when using char* pointers

struct employee {
    char *name;
    char *surname;
};

int main() {
    struct employee worker;
    worker.name = "Robert";
    worker.surname = "Woz";

    printf("\n");
    printf("%s", worker.name);
    printf("\n");
    printf("%s", worker.surname);
    printf("\n");

    char name[] = "Robert";   //declaration of array with using [] postfix

    for (int i = 0; i < 7; i++) {
        printf("%c", name[i]);
    }

    printf("\n");

    char *surname = "Woz";   //declaration of array wit using char* pointer

    for (int i = 0; i < 4; i++) {
        printf("%c", surname[i]);
    }

    printf("\n");

    return (0);
}

Program output:

Robert
Woz
Robert
Woz
chqrlie
  • 131,814
  • 10
  • 121
  • 189
Andriy Veres
  • 81
  • 1
  • 1
  • 5
  • C or C++? Choose one. – Christian Hackl Feb 19 '17 at 14:20
  • `char *surname` doesn't declare an array; it declares a pointer. – melpomene Feb 19 '17 at 14:22
  • `char *surname="Woz";` creates a string literal of static lifetime and assigns `surname` to point at it. `char name[]="Robert";` creates a character array of scoped lifetime, an array is _not the same as a pointer_ , and so an array has some extra information i.e. it's size, where as a pointer won't know how much memory it's pointing at. – George Feb 19 '17 at 14:29
  • `char []name` -> should be `char name[size]` – Seek Addo Feb 19 '17 at 14:29
  • so `char *surname="Woz";` creates pointer for first char element of array surname, but whene is determined size of memory that store surname? – Andriy Veres Feb 19 '17 at 14:34
  • `char name[] = "Robert";` works, but `char name[]; name = "Robert";` won't. The latter is what you try, with the only difference that you try to wrap it inside a `struct`. – alk Feb 19 '17 at 14:38
  • @tuple_cat yea that is when he doesn't have the intended size in mind. and it should be unsigned char[] – Seek Addo Feb 19 '17 at 14:42

3 Answers3

3

First of all, to include the standard library headers use <header.h> instead of "header.h". When using the quotes the compiler will atempt to find the header in the file directory. When using <> the compiler looks for the header in your include directory.

Secondly, the declaration using brackets accepts only char name[];, not char []name;. But if you declare this way in a structure, you need to specify the size, like char name[30];. You can only declare without a size when declaring a single variable and initializing it with a string literal, so the compiler will deduce the size from the string literal. Example: char name[] = "Robert";.

Thirdly, the difference of declaring char* name from declaring char name[30] is that in the first way you declare a pointer to a sequence of chars in the memory, while in the second way, you declare a fixed size array. Note that assigning a fixed size array to a smaller string literal (or bigger) after its initializing may result in an error, while assigning a char pointer will allow you assinging it to any size string literal.

bzim
  • 1,002
  • 9
  • 17
  • But size of array that declared in this way `char* name` will be static, or i can redeclare it by using `realloc();` function? – Andriy Veres Feb 19 '17 at 14:41
  • `char* name` does not define an array at all, but just a pointer that ***could*** point to an a `char`, which ***could*** be the 1st element of an array, ***if*** you made it point to it. – alk Feb 19 '17 at 14:43
  • For using `realloc` on anything, you first need to use `malloc` or `calloc` and assigns `malloc` return value to it. And after using the variable which you assigned to `malloc`, you need to use `free` on it. If you intend to use dynamic memory allocation with `malloc` family, then use a `char*`. If you intend to reassign the variable to different sized string literals, you should also use `char*`. – bzim Feb 19 '17 at 14:46
  • So `char *surname="Woz";` is equivalent (in way, that will be created the similar static arrays with 4 elements) `char surname[4]="Woz";`? – Andriy Veres Feb 19 '17 at 14:47
  • It is equivalent, but using `char surname[4] = "Woz"` will create a rigid size array, while `char * surname = "Woz"` can be used dynamically. – bzim Feb 19 '17 at 14:49
  • A `char*` is something that points to a sequence of chars in the memory, while a `char name[]` is the sequence itself. – bzim Feb 19 '17 at 14:51
  • i know that if you want to create, an array, that store strings with different size you need to use `malloc();`, `realloc();` and in the end `free();`, with the pointer `char *surname;`, but i find, that works another way to change the array size `char* surname="robert"; surname="robertrobertrobert";` works also and array (i suppose) also dynamic, like when we using a `malloc(); realloc(); free();` or no? – Andriy Veres Feb 19 '17 at 14:56
  • Yes. But with `char * name = "robert";`, the automatic memory allocation is used, while with malloc family the user allocated memory is used. Don't mix the two things; if you used malloc first, and then you want to assign the pointer to a string literal, use free first. – bzim Feb 19 '17 at 15:03
  • Thanks a lot, no clear. Do You know some books about automatic memory allocation, or maybe links for more detailed explanation? – Andriy Veres Feb 19 '17 at 15:16
  • http://stackoverflow.com/questions/8385322/difference-between-static-memory-allocation-and-dynamic-memory-allocation – bzim Feb 19 '17 at 15:20
  • I think that the answer to this question is well explained – bzim Feb 19 '17 at 15:20
  • Thanks a lot, deeply understand, so my conclusion in the end, if You need in a structure to store char string, that can have different size, the best way - use automatic memory allocation. – Andriy Veres Feb 19 '17 at 15:26
1

If you use char name[] in a struct, your compiler don't know the size of the char tab. That's why you have to use a pointer in your struct type, or you could use

char name[MAX_SIZE]; in your struct type.

But I recommand using pointers :)

Dot31
  • 11
  • 2
  • But pointer its only a variable, that stored memory address, vs worker.surname stored array, that have a some quantity of char elements, when is reserved memory for string? In this moment: `worker.surname = "Woz";`? – Andriy Veres Feb 19 '17 at 14:38
  • array name is a pointer, that store address of first array element, or no? – Andriy Veres Feb 19 '17 at 14:39
0

According to your question.

  1. Header file quotes. < > instead of " "
  2. I don't think that , C allows the Array declaring like Java do. char name[] = " "; instead of char []name=" ";

    *name is simply a pointer.