0

i am trying to make a array of pointer to char. I decided to use typedef definicion, but i have no clue what am doing wrong...

typedef struct _pp{
    char* a[10];
    int b;
}pp;

int main(){
  pp *taa = (pp* )malloc(sizeof(pp));
  taa->b = 2;
  printf("%d\n", taa->b);
  taa->a[1]=(char* )malloc(strlen("Peter")+1);
  strcpy((taa->*a[1]), "Peter");

  printf("%s\n", taa->*(a[1]));
  /*

/That works/

  int i;  
  int* a[5];
  for(i=0;i<5;i++){
    a[i]=(int* )malloc(sizeof(int));
    **(a+i)=i+100;
    printf("%d\n", **(a+i));
  */}

EDITED

Is that good practice?

  for(i=0;i<10;i++){
    taa->a[i]=(char* )malloc(strlen("Peter")+1);
    strncpy((taa->a[i]), "Peter", strlen("Peter"));
  }
  for(i=0;i<10;i++){
    printf("%s\n", taa->a[i]);
  }

QUESTION 3) taa->*a[1] is equivalent to ta.***(a + i) ?

printf("%c",*taa->a[1]);  It dereference 'P' character, how i can get acces to 'e'?

printf("%c",*(taa->a[1]+0));

printf("%c",*(taa->a[1]+1)); That's the way...

Xos Lamoo
  • 17
  • 2
  • 7
  • 1
    Well, what error are you getting, and on what line of code? – OldProgrammer Nov 13 '13 at 20:16
  • Although C++ has the `->*` operator I don't think that it is available in C... – nonsensickle Nov 13 '13 at 20:22
  • it is, well at least on my compiler , – user2533527 Nov 13 '13 at 20:38
  • If this is C (as tagged), [please don't cast the return value of `malloc()`](http://stackoverflow.com/a/605858/28169). – unwind Nov 13 '13 at 20:39
  • @user2533527 What compiler are you using? Because http://en.wikipedia.org/wiki/Operators_in_C_and_C++#Member_and_pointer_operators disagrees with what you say... It is available in C++ though and does something different. – nonsensickle Nov 13 '13 at 20:43
  • I am casting, because it is transparent. – Xos Lamoo Nov 13 '13 at 20:46
  • @nonsensickle I used to use Microsoft compiler on C and it eats the -> operator but I think you might be right about other compilers since Microsoft C compiler is actually C++ compiler. also in Microsoft DDK I used to use -> operator . – user2533527 Nov 14 '13 at 02:08

2 Answers2

1

Try:

strcpy((taa->a[1]), "Peter");

Also note that [1] is accessing the second element in the array; use [0] for the first. Also best to use strncpy or something "safe" which you pass the length of your char buffer (string) into to stop memory being scribbled on is good.

Edit:

Safe string functions that aren't standard:

strlcpy

strcpy_s

noelicus
  • 14,468
  • 3
  • 92
  • 111
  • `strcpy_s` is not standard C – user4815162342 Nov 13 '13 at 20:22
  • @user4815162342: `strcpy_s()` is an optional part of C11 — ISO/IEC 9899:2011 (Annex K, normative). However, the only platform where it is readily available is Microsoft Windows with MSVC. – Jonathan Leffler Nov 13 '13 at 20:24
  • I think that it applies only to the `gets()` function http://en.wikipedia.org/wiki/C11_%28C_standard_revision%29 – nonsensickle Nov 13 '13 at 20:24
  • @nonsensickle: What 'only applies to the `gets()` function'? – Jonathan Leffler Nov 13 '13 at 20:25
  • @JonathanLeffler Point taken. Changed to strncpy!! – noelicus Nov 13 '13 at 20:26
  • @JonathanLeffler I read things quite literally. The wiki article only mentions `gets()`, although I would agree that it was "most likely" propagated to the rest I can't say for sure. Do you have a reference? – nonsensickle Nov 13 '13 at 20:27
  • @nonsensickle The wiki article is not the standard - see [the actual standard](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf), §K.3.7.1.3 (page 615). – user4815162342 Nov 13 '13 at 20:46
  • @noelicus: About strncpy... I recommend not using it as a replacement for strcpy for performance reasons. strncpy will zero-fill the entire buffer. If this is something like an 8KB buffer for holding a URL, which is usually around 20 bytes, that is a big waste of time. Yes, it shows up in performance profiling. If you can use strlcpy or you can use snprintf. – Zan Lynx Nov 13 '13 at 20:48
  • @user4815162342 I'm aware it is not the standard but I wanted an actual reference. Thanks for providing it :) – nonsensickle Nov 13 '13 at 20:51
  • See also [Do you use the TR 24731 'safe' functions?](http://stackoverflow.com/questions/372980/do-you-use-the-tr-24731-safe-functions), where TR24731-1 was technical report that was a precursor to Annex K in the 2011 standard. Annex K covers a lot of functions. The `gets()` function is simply eliminated from §7.21.7 (where `fgets()` is still documented). The `gets_s()` function is documented in §K.3.5.4.1. – Jonathan Leffler Nov 13 '13 at 20:51
0

strcpy function needs a char* type for first argument. in your pp struct you have ten char*s. you are accessing your second char* with taa->a[1]. So removing * after -> will give you what you need. This works fine:

strcpy((taa->a[1]), "Peter");
Fredrick Gauss
  • 5,126
  • 1
  • 28
  • 44
  • You mean aside from `->*` being illegal syntax in C? – nonsensickle Nov 13 '13 at 20:42
  • taa->*a[1] is equal to ta.***(a + i) i thing... There are three level of decrement.. – Xos Lamoo Nov 13 '13 at 20:44
  • @nonsensickle, as i remember, [MinGW](http://www.mingw.org/) complains about `->*` but `*taa->a[i]` is ok. @XosLamoo I could not figure out what `***(a+i)` is about this subject. – Fredrick Gauss Nov 13 '13 at 20:53
  • @FredrickGauss You are correct. Sorry, it seems I forgot my manners. I've been saying it is not C without offering an alternative. Yes, you are meant to use `*taa->a[i]` to dereference `a[i]`. The `->` operator has higher precedence than `*` so it is equivalent to `*(taa->a[i])` which is what I think is desired. The `->*` operator is used for member function pointers in C++. This http://aristeia.com/Papers/DDJ_Oct_1999.pdf article should give an example as to what the `->*` operator is used for in C++. – nonsensickle Nov 13 '13 at 20:59
  • I am meant to use `*taa->a[i]` to dereference `a[i][0]` that is `taa->a[i][0]` for single `char`acter access. But for the question, it is the address of the character array so simply `taa->a[i]`. – Fredrick Gauss Nov 13 '13 at 21:06
  • @fredrick-gauss This is not working. `while(*taa->a[i++]) printf("%c",*taa->a[i]);` How to dereference single char? – Xos Lamoo Nov 13 '13 at 21:16
  • you can not dereference when `i = 10` in `while(*taa->a[10])...` – Fredrick Gauss Nov 13 '13 at 21:24
  • @FredrickGauss printf("%c",*(taa->a[1]+0)); printf("%c",*(taa->a[1]+1)); That's the way? – Xos Lamoo Nov 13 '13 at 21:46
  • `taa->a[i][1]` or `*(taa->a[1] + 1)` will lead to the `'e'` in the `"Peter"` as of second character. – Fredrick Gauss Nov 13 '13 at 22:15