-1

Ok, so I have an assignment that gives me this constant:

const char *suits[] = {"Hearts", "Diamonds", "Clubs", "Spades"};

basically it is just a table pointer that points to 4 words! Simple right?

then what i have to do is to import each word to another table!

So i create a new table:

char table[30];

in the main structure, and i want somehow to import inside the word "Diamonds"! On the pointer's table this word is on second place. So it is suits[1].

Well when try to print the second word using this command:

printf("%s", *suits[1]);

i get error. but using that command

printf("%c", *suits[1]);

i only get "D", which is only the first letter. So do you have any ideas on how i would be able to print the whole word "Diamonds", and how can i copy it to the table that i create in the main form?

(i just need to copy the word from suits[1] to new table and then be able to print the table)

THANK YOU VERY MUCH!!!

crashmstr
  • 28,043
  • 9
  • 61
  • 79
Andrei Cusnir
  • 2,735
  • 1
  • 14
  • 21
  • You should make sure you tag language. It helps people to find your question and also affects syntax coloring. I've added the C tag, since your use of raw arrays, C-style strings, and `printf` would indicate that language. Also of note: the table tag says "do not use". It has been removed. – crashmstr Oct 06 '14 at 19:10
  • I am really sorry that i didn't mention that, it is C! – Andrei Cusnir Oct 06 '14 at 19:10
  • @AndreasCusnir How come you call a char array a ‘table’? – Biffen Oct 06 '14 at 19:11
  • It is a table that contains characters :D if i write "int table[10];", then it will only save numbers. – Andrei Cusnir Oct 06 '14 at 19:12
  • @AndreasCusnir That's called an ‘array’. – Biffen Oct 06 '14 at 19:13
  • That's right, i am sorry! You know the University of Cyprus is not doing a really great job on giving correct names! So sometimes we call it table – Andrei Cusnir Oct 06 '14 at 19:14

2 Answers2

1

I'm not clear on what you mean by "copy to the table", but on printing the string, I can certainly help there.

Executive Summary:

printf("%s", suits[0]); // Prints Diamonds

What is a string in C, and what does the memory look like?

We have the code:

const char *suits[] = {"Hearts", "Diamonds", "Clubs", "Spades"};

In memory this is:

suits[0] -> A pointer to memory that contains {'H', 'e', 'a', 'r', 't', 's', '\0'}
suits[1] -> A pointer to memory that contains {'D', 'i', 'a', 'm', 'o', 'n', 'd', 's', '\0'}
...

A string in C usually refers to a pointer to a null terminated piece of memory. So when we want to print a string, we do:

printf("%s", <a pointer to a null terminated string>);

In this case, the pointer is found at suits[0], so we do:

printf("%s", suits[0]);

Thoughts on copying to the table.

If you simply want to store the string "Hearts" in that array, then perhaps you're looking for:

snprintf(table, sizeof(table), "%s", suits[0]);

or

strncpy(table, sizeof(table), suits[0]);
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
0

To print each of the strings use printf("%s",suits[i]);. You do not need to dereference the pointer. Check this answer for better understanding of how your array is stored in the memory.

Community
  • 1
  • 1
Sinstein
  • 887
  • 11
  • 42
  • Yeap! That's working on printing! Thank you for that, but i am really looking to copy the word into new array which i will determin in the main form, and then print the array that will have the word "Diamonds" inside, which is located at suits[1] – Andrei Cusnir Oct 06 '14 at 19:17
  • Check this answer : http://stackoverflow.com/a/16645642/2651076 for what you want to achieve. You cannot directly assign content of one char array to another. – Sinstein Oct 06 '14 at 19:23
  • Thank you!!! i was loooking for "snprintf(table, sizeof(table), "%s", suits[0]);" which @sharth give answer – Andrei Cusnir Oct 06 '14 at 19:25