Zero matrix in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ExoScript
    New Member
    • Sep 2015
    • 1

    Zero matrix in C

    Hi All!

    I need to create zero matrix. I guess that I should allocate array of pointers on double/float/int and then allocate arrays for each pointer? And than I should fill all arrays by zero element. Is it correct?

    Thanks!
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Read this first: http://bytes.com/topic/c/insights/77...rrays-revealed

    Create your array and then set each element to zero using a loop.

    Comment

    • CoddingHead
      New Member
      • Sep 2015
      • 2

      #3
      Hi
      There is no necessary to set each element to zero using a loop(of cource if you're using dynamic memory).
      And I guess that the more propper way is to declare matrix like a struct.

      Comment

      • CoddingHead
        New Member
        • Sep 2015
        • 2

        #4
        By default it will be initiated like a zero

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          @CoddingHead, in C variables are automatically initialized to zero at program start-up, but only if they have static storage duration and don't have an explicit initializer. The following kinds of variables do not have static storage duration (and hence are not automatically initialized to zero):
          • Automatic variables (defined within a block, but without static keyword).
          • Register variables (defined with register keyword).
          • Dynamic variables (created with malloc or calloc).

          I believe the rules are similar in C++. I suppose variables created with new keyword are perhaps maybe like dynamic variables. I'm not a C++ expert.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Built-in types in C++ have the same rules as C. User-defined types rely on constructors for initialization.

            Comment

            Working...