C - 函数(假设a和n是参数,其中a是int值的数组,n是数组的长度。)

时间:2018-03-05 04:55:22

标签: c

我是编程新手,我真的不明白这个问题。你们当中有些人可以举例说明这意味着什么。如何编写一个函数,其中a是int值,n是长度?

我很困惑......

2 个答案:

答案 0 :(得分:1)

我不确定您的问题是什么,因为您还没有提供太多信息。但是,C中的函数定义如下:

void arrayFunction( int a[], int n){
    //do whatever you need to do with the function here
}

所以,对于这种情况,我们可以说:

from sqlalchemy import inspect

class Event(Base):
    __tablename__ = 'tbl_events'
    event_id = Column(Integer, primary_key=True)
    event_type_id = Column(String(50))  # 0 is Match, 1 is Competition

    __mapper_args__ = {'polymorphic_on': event_type_id}

    @classmethod
    def from_id(cls, id, session):
        mapper = inspect(cls).mapper
        mapper.polymorphic_map[cls.EVENT_TYPE_ID] = mapper
        mapper.polymorphic_identity = cls.EVENT_TYPE_ID

        return session.query(cls).filter_by(event_id=id).one()

class Match(Event):

    EVENT_TYPE_ID = 0

    __tablename__ = 'tbl_match_details'

    event_id = Column(Integer,
                      ForeignKey('tbl_events.event_id'),
                      primary_key=True)
    team_1 = Column(String(50))
    team_2 = Column(String(50))

    __mapper_args__ = {'polymorphic_identity': EVENT_TYPE_ID}

这可能对你有帮助。

答案 1 :(得分:0)

假设您有一个int数组,如下所示:

arr

您可以看到数组arr内部有5个元素。你可以数数吧。 但是,当您将上述arr传递给函数时,该函数无法知道#include <stdio.h> void display(int arr[]){ } int main(void) { int arr[] = {2,3,4,5,6}; display(arr); return 0; } 包含多少元素。请参阅下面(不正确的)代码段:

display()

名为&#39; arr&#39;的功能不知道n有多少元素

因此,您需要传递额外的参数(名为&#39; arr&#39;)的额外参数来告诉该被调用函数有关arr内元素的数量。你需要单独告诉它 - arr的长度。

现在这变成了 - 正如你在问题中所说的那样 - n是int值,#include <stdio.h> void display(int a[], int n){ //Now display knows about lenth of elemnts in array 'a' // Length is 5 in this case } int main(void) { int arr[] = {2,3,4,5,6}; display(arr, 5); return 0; } 是长度

以下是正确的代码:

display()

现在,名为&#39; public class SequenceEncoder { private SeekableByteChannel ch; private Picture toEncode; private RgbToYuv420 transform; private H264Encoder encoder; private ArrayList<ByteBuffer> spsList; private ArrayList<ByteBuffer> ppsList; private FramesMP4MuxerTrack outTrack; private ByteBuffer _out; private int frameNo; private MP4Muxer muxer; public SequenceEncoder(File out) throws IOException { this.ch = NIOUtils.writableFileChannel(out); // Transform to convert between RGB and YUV transform = new RgbToYuv420(0, 0); // Muxer that will store the encoded frames muxer = new MP4Muxer(ch, Brand.MP4); // Add video track to muxer outTrack = muxer.addTrackForCompressed(TrackType.VIDEO, 1); // Allocate a buffer big enough to hold output frames _out = ByteBuffer.allocate(1920 * 1080 * 6); // Create an instance of encoder encoder = new H264Encoder(); // Encoder extra data ( SPS, PPS ) to be stored in a special place of // MP4 spsList = new ArrayList<ByteBuffer>(); ppsList = new ArrayList<ByteBuffer>(); } public void encodeImage(Bitmap bi) throws IOException { // encodeNativeFrame(AWTUtil.fromBufferedImage(bi)); encodeNativeFrame(fromBitmap(bi)); } public void encodeNativeFrame(Picture pic) throws IOException { if (toEncode == null) { toEncode = Picture.create(pic.getWidth(), pic.getHeight(), ColorSpace.YUV420); } // Perform conversion transform.transform(pic, toEncode); // Encode image into H.264 frame, the result is stored in '_out' buffer _out.clear(); ByteBuffer result = encoder.encodeFrame(_out, toEncode); // Based on the frame above form correct MP4 packet spsList.clear(); ppsList.clear(); H264Utils.encodeMOVPacket(result, spsList, ppsList); // Add packet to video track outTrack.addFrame(new MP4Packet(result, frameNo, 1, 5, frameNo, true, null, frameNo, 0)); frameNo++; } public void finish() throws IOException { // Push saved SPS/PPS to a special storage in MP4 outTrack.addSampleEntry(H264Utils.createMOVSampleEntry(spsList, ppsList)); // Write MP4 header and finalize recording muxer.writeHeader(); NIOUtils.closeQuietly(ch); } public static Picture fromBitmap(Bitmap src) { Picture dst = Picture.create((int) src.getWidth(), (int) src.getHeight(), ColorSpace.RGB); fromBitmap(src, dst); return dst; } public static void fromBitmap(Bitmap src, Picture dst) { int[] dstData = dst.getPlaneData(0); int[] packed = new int[src.getWidth() * src.getHeight()]; src.getPixels(packed, 0, src.getWidth(), 0, 0, src.getWidth(), src.getHeight()); for (int i = 0, srcOff = 0, dstOff = 0; i < src.getHeight(); i++) { for (int j = 0; j < src.getWidth(); j++, srcOff++, dstOff += 3) { int rgb = packed[srcOff]; dstData[dstOff] = (rgb >> 16) & 0xff; dstData[dstOff + 1] = (rgb >> 8) & 0xff; dstData[dstOff + 2] = rgb & 0xff; } } } } &#39;的功能知道int数组的长度。这是您在指定数组及其长度时编写代码的方式。

更正式地说,这是因为在传递数组时,它会衰减到一个指针,因此需要同时传递它的长度。