1

I have the following C code:

person->title

The title is a three byte char array. All I want to do is print those three bytes. I can't use the string formatter with printf because there is no null byte.

How can I print this out without having to copy it or add a null byte to it?

Thanks in advance!

UPDATE

Turns out it's not an array, just three adjacent bytes. If I try to compile with the code suggested:

person->title[0]

It fails with the error:

test.c:46: error: subscripted value is neither array nor pointer.
PeterM
  • 2,534
  • 6
  • 31
  • 38
  • 1
    Exact Duplicate: http://stackoverflow.com/questions/2137779/how-do-i-print-a-non-null-terminated-string-using-printf – Kiril Kirov Mar 08 '11 at 07:05
  • Except that solution doesn't work because printf still looks for the null byte. Using that solution ( I tried it before ) causes a segfault in strnlen(). I hope I'm missing something because that would be an awesome solution... – PeterM Mar 08 '11 at 07:21
  • If you can expand your arrays to account for a terminating, '\0' character. This will allow you to use the string functions instead of writing your own array accessing functions. I believe one extra character is worth the saving of development time. – Thomas Matthews Mar 08 '11 at 17:53

4 Answers4

3
printf("%c%c%c",person->title[0],person->title[1],person->title[2]);
Ovidiu Pacurar
  • 8,173
  • 2
  • 30
  • 36
1

You could use something like:

printf("%c%c%c", person->title[0], person->title[1], person->title[2]);

if you are sure the three bytes are filled. If it could be shorter than three bytes, try something like:

for (int i=0; (i<3) && (person->title[i] != 0); i++)
  printf("%c", person->title[i]);
Mat
  • 202,337
  • 40
  • 393
  • 406
  • It's not null-terminated, so even if it's shorter than 3 bytes, the second check is useless (: – Kiril Kirov Mar 08 '11 at 07:07
  • I was thinking of the case where it could hold "Mrs", "Mr.", or sometimes just "M." for instance. i.e. not null-terminated in the normal sense, but potentially shorter than 3 chars total. – Mat Mar 08 '11 at 07:11
1

Please try this....

char *a = &(person->title);
printf("%c %c %c", *a, *(a+1) , *(a+2));

EDIT

Based on 20 fields in the struct it`s much more simple mate, contiguous memory allocation takes place in case of struct

eg:

struct test {
 // Suppose the elements of the struct are only title and name each having 3 bytes
}*person;

// The first 3 bytes are w.r.t title and the very next 3 is allocated to name, note for 
// struct it`s stored as contiguous memory allocation


// code can be rewritten as - to display title and name

char *a = &(person->title);
printf("Title : %c%c%c", *a, *(a+1), *(a+2));
printf("Name : %c%c%c", *(a+3), *(a+4), *(a+5));
NirmalGeo
  • 773
  • 4
  • 12
0

For structs - you could exploit the fact that the memory will be allocated as contiguous bytes. You could also use fwrite() to control exactly how many bytes are written (using "stdout" as file pointer if you want it printed to the terminal).

e.g.

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

typedef struct _test
{
  char title[3];
  char job[4];
  char other[5];
} Test;

int main(int argc, char* argv)
{
  Test test;

  strcpy(test.title,"MR_");
  strcpy(test.job,"1234");
  strcpy(test.other,"OTHER"); 

  fwrite(&test,1,sizeof(Test),stdout);
  printf("\n");
}

Will output:

MR_1234OTHER
timlukins
  • 2,694
  • 21
  • 35