0

I have Data.C:

#include "Data.h"

char data_a[999][999];

Data.h:

#ifndef Data_h
#define Data_h

#include <stdio.h>
extern char data_a[999][999];
#endif /* Data_h */

And then Main.c:

#include "Data.h"

char *s;
...

printf("Write -%s- to -%d-\n",s,counter_a);
printf("BEFORE -%c-\n",data_a[counter_a][0]);
data_a[counter_a][0]=*s;
printf("AFTER  -%c-\n",data_a[counter_a][0]);

And when Main.c is run this is what is printed as an example:

Write - This is my sample string I want to save to the array at position 166 
- to -166-
BEFORE --
AFTER  - -

What I am trying to do is have data_a[166][0] be equal to the value of s which should be "This is my sample string I want to save to the array at position 166". I know I have something not quite right, just need a nudge in the right direction.

ez4nick
  • 9,756
  • 12
  • 37
  • 69
  • 2
    The expression `*s` is *exactly* equal to `s[0]`. It's a single character. Which you already know since you use `%c` to print that single character. So you're writing the leading space in the string that `s` is pointing to. So I'm kind of confused as to what your problem might be? What is the output you *expect* to get? – Some programmer dude Jun 30 '23 at 16:08
  • For `data_a[166][0]` to *"be equal"* to a string of characters, `data` would have to be a three-dimensional array of `char` **OR** a two-dimensional array of `char *`. – Oka Jun 30 '23 at 16:11
  • 1
    You *probably* want `strcpy(data_a[counter_a], s)`. – Oka Jun 30 '23 at 16:13
  • You can also put the entire character arrays (not just pointers) in structs; *those* you can assign (including a deep array copy). Effectively the rudiment of a poor man's string class. – Peter - Reinstate Monica Jun 30 '23 at 16:15
  • My expectation was that `data_a[166][0]` would be equal to `"This is my sample string I want to save to the array at position 166"`. So I think based on the comments my misunderstanding is that `data_a[166][0]` is actually a single character and not a string of characters? – ez4nick Jun 30 '23 at 16:16
  • Yes, because `char` is a single byte. – Oka Jun 30 '23 at 16:17
  • @ez4nick Indeed, it is a single character. That can be seen from the declaration: `char data_a[999][999];` decfines an object data_a that, indexed two times, has the type char. Indexing two times is what you do; a char is what you get. ;-). – Peter - Reinstate Monica Jun 30 '23 at 16:18
  • If you index `data_a` one time only you get a one-dimensional array which is as close to a string as you can get in C. Alas, arrays are not assignable. – Peter - Reinstate Monica Jun 30 '23 at 16:19
  • Appreciate all the suggestions, I was able to figure it out. The declaration I needed was `char *data_a[999][999];`. – ez4nick Jun 30 '23 at 17:11
  • The format specifier `-%s-` outputting `- This`... tells you that the string does not begin with a `T` as in `"This`... but rather a space character as in `" This`... Your example lacks the details about how the string was set in the first place. Regardless, `data_a[counter_a][0]=*s;` is just moving one char, not an entire string. – Wyck Jun 30 '23 at 18:40
  • Are you sure [`char *data_a[999][999]`](https://cdecl.org/?q=char+*data_a%5B999%5D%5B999%5D) is what you want? – Neil Jun 30 '23 at 19:16

1 Answers1

1
data_a[counter_a][0]=*s;

This is saving the first character of s to the character at data_a[counter_a][0]. It doesn't affect any other character. Attempting to print this likely results in undefined behavior unless data_a[counter_a][1] is '\0'. Without that null terminator data_a[counter_a] is a character array, but is not a string.

To copy the entirety of s into data_a[counter_a] you would use strcpy or more correctly strncpy as you know the bounds of data_a[counter_a].

Chris
  • 26,361
  • 5
  • 21
  • 42
  • 2
    It might be a 1-character string left over from the static initialization of 0 at the second character, (but this isn't what the OP intended.) – Neil Jun 30 '23 at 19:30