Thread: Why can't i allocate char *array? or other problem?

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    77

    Why can't i allocate char *array? or other problem?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int min(int a, int b)
    { return (a <= b ? a : b); }
    
    void init(char *a, char *b, int lengthA, int lengthB)
    {
      char s[4] = {'A', 'C', 'G', 'T'};
      FILE *fData;
      int i;
      printf("%d", 4);
      fData = fopen("longest-common-subsequence.dat", "wt");
      for (i = 0; i < lengthA; i++)
        a[i] = s[rand()%4];
      for (i = 0; i < lengthB; i++)
        b[i] = s[rand()%4];
      fprintf(fData, "%d\n%d\n", lengthA, lengthB);
      for (i = 0; i < lengthA; i++)
        fprintf(fData, "%s", a[i]);
      fprintf(fData, "\n");
      for (i = 0; i < lengthB; i++)
        fprintf(fData, "%s", b[i]);
      fclose(fData);
    }
    
    int main()
    {
      char *x, *y, *lcs;
      int lengthA, lengthB;
      printf("lengthA = ");
      scanf("%d", &lengthA);
      printf("lengthB = ");
      scanf("%d", &lengthB);
      x = malloc(lengthA*sizeof(*x));
      y = malloc(lengthB*sizeof(*y));
      lcs = malloc(min(lengthA, lengthB)*sizeof(*lcs));
      init(x, y, lengthA, lengthB);
      return 0;
    }
    The program stoped after char *x, *y and *lcs were allocated... I wanna know what's matter go wrong. and how to declare constant array in C? Thanks.
    Last edited by Mathsniper; 06-13-2005 at 10:36 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't use %s to refer to a single character. %s is for strings. %c is for single characters.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    77
    Okay, I have changed it. um... could you tell me how to declare constant array?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What do you mean? Do you mean a fixed size? Constant in that you can't change its contents? What exactly?


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    77
    Quote Originally Posted by quzah
    What do you mean? Do you mean a fixed size? Constant in that you can't change its contents? What exactly?


    Quzah.
    yes, i wanna array can't be changed its contents. like this mean, #define array.

  6. #6
    ---
    Join Date
    May 2004
    Posts
    1,379
    Code:
    char *const_array = "Hello";

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    77
    Quote Originally Posted by sand_man
    Code:
    char *const_array = "Hello";
    no, my mean that make constant array like this, char const_array[n] = {'a1', 'a2', ..., 'an'}

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    Code:
    const char *string_array = "Hello";
    
    printf("String is: %s\n",string_array);


    Code:
    const char *array_of_strings[] = { 
    	"abc",
    	"def",
    	"ghi",
    	"jkl"
    };
    
    for (i = 0;i<4; i++)
    {
       printf("String[%d] is: %s\n",i, array_of_strings[i]);
    }
    Program Output:
    String is: Hello
    String[0] is: abc
    String[1] is: def
    String[2] is: ghi
    String[3] is: jkl

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by nkhambal
    Code:
    const char *string_array = "Hello";
    
    printf("String is: %s\n",string_array);


    Code:
    const char *array_of_strings[] = { 
    	"abc",
    	"def",
    	"ghi",
    	"jkl"
    };
    
    for (i = 0;i<4; i++)
    {
       printf("String[%d] is: %s\n",i, array_of_strings[i]);
    }
    You have a slight problem. Add this:

    Code:
    for (i = 0;i<4; i++)
        array_of_strings[ i ] = "hello";
    
    
    for (i = 0;i<4; i++)
    {
       printf("String[%d] is: %s\n",i, array_of_strings[i]);
    }
    Then run it.


    Now try this instead:
    Code:
    char * const array[] = { "abc", "def", "ghi", "jkl" };
    This works here "correctly", because you're already assigning string literals, so you can't edit the strings anyway. So you set the pointers as constant, so now you can't change the contents of this array.

    [edit]
    For those of you who don't understand what's going on, I'll clarify a bit.

    Code:
    char *s;
    This is a pointer to a character. Read it from right to left: "S is a pointer to a character."

    This is a pointer to a character assigned to a single character:
    Code:
    char c = 'a';
    char *s = &c;
    You can use this pointer to a character to point to arrays if you like:
    Code:
    char array[] = { 'a', 'b', 'c', 'd', 'e' };
    char * s = array;
    We can now use the pointer to move through the array, or even change its values:
    Code:
    while( *s != 'e' )
    {
        printf("*s is %c", *s );
        *s = toupper( *s );
        printf(", but now *s is %c\n", *s );
    }
    You can make it point to a string too:
    Code:
    char *s = "this is a string literal";
    Now the deal with string literals is that you cannot change the literal iteself. That is to say, we cannot change the 'e' to an 'a' and make the string say "this is a string lateral". String literals are stored in read only memory, and you're not allowed to change them.

    However! The pointer itself is not constant. You can change what the pointer points at. Not the contents of what it points at, in the case of a literal, but what the pointer itself points to:
    Code:
    char *s = "this is a string literal"
    
    printf("s is \'%s\'\n", s );
    
    s = "now a new literal is being pointed at by s!";
    printf("s is \'%s\'\n", s );
    As stated above, since this is in fact a string literal, you can't edit the literal iteself. But since they (the poster before me) didn't actually make the pointer constant, you can change it. (Thus they didn't fulfill what the OP was looking for. (ie: The array's contents can be changed.))
    [/edit]


    Quzah.
    Last edited by quzah; 06-14-2005 at 09:57 AM.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Apr 2005
    Posts
    77
    still get problem...
    Code:
    void init(char *a, char *b, int lengthA, int lengthB)
    {
      char * const s[] = {'A', 'C', 'G', 'T'};
      int lengthS = 4;
      FILE *fData;
      int i = 0;
      fData = fopen("longest-common-subsequence.dat", "wt");
      while (s[i] != '\0') i++;
      printf("%c\n", i);
      for (i = 0; i < lengthA; i++)
        a[i] = s[rand()%lengthS];
      for (i = 0; i < lengthB; i++)
        b[i] = s[rand()%lengthS];
      fprintf(fData, "%d\n%d\n", lengthA, lengthB);
      for (i = 0; i < lengthA; i++)
        fprintf(fData, "%c", a[i]);
      fprintf(fData, "\n");
      for (i = 0; i < lengthB; i++)
        fprintf(fData, "%c", b[i]);
      fclose(fData);
    }
    Code:
    longest-common-subsequence.c: In function `init':
    longest-common-subsequence.c:10: warning: initialization makes pointer from integer without a cast
    longest-common-subsequence.c:10: warning: initialization makes pointer from integer without a cast
    longest-common-subsequence.c:10: warning: initialization makes pointer from integer without a cast
    longest-common-subsequence.c:10: warning: initialization makes pointer from integer without a cast
    longest-common-subsequence.c:16: warning: assignment makes integer from pointer without a cast
    longest-common-subsequence.c:18: warning: assignment makes integer from pointer without a cast
    why does gcc compiler ask me that pointer is made to be integer?

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Try
    Code:
    static const char s[] = {'A','C','G','T'};
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    why does gcc compiler ask me that pointer is made to be integer?
    Because you weren't using strings, you were using single characters, which are an integral type.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  2. problem with reading and writing
    By yahn in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2006, 04:38 PM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. String Processing Problem
    By muffin in forum C Programming
    Replies: 0
    Last Post: 08-24-2001, 10:13 AM